# 131
class Solution:
def partition(self, s: str) -> List[List[str]]:
n = len(s)
dp = [[False] * n for _ in range(n)]
for i in range(n):
dp[i][i] = True
for j in range(1, n):
for i in range(j):
if s[i] == s[j]:
if j - i < 3:
dp[i][j] = True
else:
dp[i][j] = dp[i + 1][j - 1]
else:
dp[i][j] = False
res = []
def fn(i, tmp):
if i == n:
res.append(tmp[:])
else:
for j in range(i, n):
if dp[i][j]:
fn(j + 1, tmp + [s[i: j + 1]])
fn(0, [])
return res
# 132
class Solution:
def minCut(self, s: str) -> int:
dp = [len(s) for i in range(len(s) + 1)]
dp[0] = -1
for i in range(len(s)):
for j in range(i + 1):
if s[j : i + 1] == s[j : i + 1][::-1]:
dp[i + 1] = min(dp[i + 1] , dp[j] + 1)
return dp[-1]
# 133
class Solution(object):
def __init__(self):
self.visited = {}
def cloneGraph(self, node):
"""
:type node: Node
:rtype: Node
"""
if not node:
return node
# 如果该节点已经被访问过了,则直接从哈希表中取出对应的克隆节点返回
if node in self.visited:
return self.visited[node]
# 克隆节点,注意到为了深拷贝我们不会克隆它的邻居的列表
clone_node = Node(node.val, [])
# 哈希表存储
self.visited[node] = clone_node
# 遍历该节点的邻居并更新克隆节点的邻居列表
if node.neighbors:
clone_node.neighbors = [self.cloneGraph(n) for n in node.neighbors]
return clone_node
# 134
class Solution:
def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:
if sum(gas) < sum(cost):
return -1
res , tmp1 , tmp2 , tmp = 0 , 0 , 0 , 0
for i in range(len(gas)):
tmp1 += gas[i]
tmp2 += cost[i]
while tmp1 < tmp2 and tmp <= i:
tmp1 -= gas[tmp]
tmp2 -= cost[tmp]
tmp += 1
res = tmp
return res
# 135
class Solution:
def candy(self, ratings: List[int]) -> int:
n = len(ratings)
dp = [1] * n
for i in range(1 , n):
if ratings[i] > ratings[i - 1]:
dp[i] = dp[i - 1] + 1
for i in range(n - 2 , -1 , -1):
if ratings[i] > ratings[i + 1]:
dp[i] = max(dp[i + 1] + 1 , dp[i])
return sum(dp)
# 136
class Solution:
def singleNumber(self, nums: List[int]) -> int:
hashmap = Counter(nums)
for k in hashmap.keys():
if hashmap[k] == 1:
return k
# 137
class Solution:
def singleNumber(self, nums: List[int]) -> int:
hashmap = Counter(nums)
for k in hashmap.keys():
if hashmap[k] == 1:
return k
# 138 抄的答案0 0 题都看不懂
"""
# Definition for a Node.
class Node:
def __init__(self, x: int, next: 'Node' = None, random: 'Node' = None):
self.val = int(x)
self.next = next
self.random = random
"""
class Solution(object):
def copyRandomList(self, head):
"""
:type head: Node
:rtype: Node
"""
if not head:
return head
ptr = head
while ptr:
new_node = Node(ptr.val, None, None)
new_node.next = ptr.next
ptr.next = new_node
ptr = new_node.next
ptr = head
while ptr:
ptr.next.random = ptr.random.next if ptr.random else None
ptr = ptr.next.next
ptr_old_list = head # A->B->C
ptr_new_list = head.next # A'->B'->C'
head_old = head.next
while ptr_old_list:
ptr_old_list.next = ptr_old_list.next.next
ptr_new_list.next = ptr_new_list.next.next if ptr_new_list.next else None
ptr_old_list = ptr_old_list.next
ptr_new_list = ptr_new_list.next
return head_old
# 139
class Solution:
def wordBreak(self, s, wordDict):
n = len(s)
if not wordDict:
return not s
dp = [False] * (n + 1)
dp[0] = True
for i in range(n):
for j in range(i + 1 , n + 1):
if dp[i] and s[i : j] in wordDict:
dp[j] = True
return dp[-1]
# 140
class Solution:
def wordBreak(self, s: str, wordDict: List[str]) -> List[str]:
@lru_cache(None)
def backtrack(index: int) -> List[List[str]]:
if index == len(s):
return [[]]
ans = list()
for i in range(index + 1, len(s) + 1):
word = s[index:i]
if word in wordSet:
nextWordBreaks = backtrack(i)
for nextWordBreak in nextWordBreaks:
ans.append(nextWordBreak.copy() + [word])
return ans
wordSet = set(wordDict)
breakList = backtrack(0)
return [" ".join(words[::-1]) for words in breakList]