Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9ef03f5

Browse files
committed
131
1 parent 10dcee1 commit 9ef03f5

File tree

3 files changed

+40
-18
lines changed

3 files changed

+40
-18
lines changed

‎README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@
137137
|40|[Combination Sum II](https://leetcode.com/problems/combination-sum-ii/#/solutions)| [Python ](./backtrack/Yu/39.py) | _O(K * (2^N)_| _O(N)_ | [:tv:](https://youtu.be/HdS5dOaz-mk)|
138138
|216|[Combination Sum III](https://leetcode.com/problems/combination-sum-iii/#/description)| [Python ](./backtrack/Yu/216.py) | _O(K * (2^N)_| _O(N)_ ||
139139
|17|[Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/)| [Python ](./backtrack/Yu/17.py) | _O(N*(4^N))_| _O(N)_ | [:tv:](https://www.youtube.com/watch?v=7KZWLM9QtRA)|
140-
140+
|131|[Palindrome Partitioning](https://leetcode.com/problems/palindrome-partitioning/description/)|[Python ](./backtrack/Yu/131.py)|_O(N*(2^N))_|_O(N)_|[:tv:](https://youtu.be/UFdSC_ml4TQ)|
141141

142142
## Greedy Medium
143143
| # | Title | Solution | Time | Space | Video|

‎backtrack/Yu/131.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#131. Palindrome Partitioning
2+
3+
4+
class Solution(object):
5+
def partition(self, s):
6+
"""
7+
:type s: str
8+
:rtype: List[List[str]]
9+
"""
10+
self.res = []
11+
self.dfs(s, [])
12+
return self.res
13+
14+
def dfs(self, s, temp):
15+
if not s:
16+
self.res.append(temp[:])
17+
return
18+
19+
for i in xrange(1,len(s)+1):
20+
if self.isPali(s[:i]):
21+
temp.append(s[:i])
22+
self.dfs(s[i:], temp)
23+
temp.pop()
24+
25+
26+
def isPali(self, s):
27+
return s == s[::-1]

‎tree/Yu/101_symmetricTree.py

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,18 @@
1212
# 思路:
1313
# 这道题只要知道一个规律,就是左边Child要等于右边Child,就很容易了
1414
# 先解决Root的Edge,之后在对其他进行一个统一处理,我选择写一个Helper,也可以不写
15-
# ****************
16-
# Final Solution *
17-
# Recursive *
18-
# ****************
15+
16+
# if not left or not right: return left == right的意思是:
17+
# if not left or not right: return False
18+
# if not left and not right: return True
19+
1920
class Solution(object):
2021
def isSymmetric(self, root):
21-
"""
22-
:type root: TreeNode
23-
:rtype: bool
24-
"""
25-
if not root:
26-
return True
27-
else:
28-
return self.isEqual(root.left, root.right)
2922

30-
def isEqual(self, n1, n2):
31-
if not n1 or not n2:
32-
return n1 == n2
33-
else:
34-
return n1.val == n2.val and self.isEqual(n1.left, n2.right) and self.isEqual(n1.right, n2.left)
23+
if not root: return True
24+
def dfs(left, right):
25+
if not left or not right: return left == right
26+
if left.val != right.val: return False
27+
return dfs(left.left, right.right) and dfs(left.right, right.left)
28+
29+
return dfs(root.left, root.right)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /