Skip to content

Commit 8c6a355

Browse files
committed
regular leetcode
1 parent 8ecbc80 commit 8c6a355

10 files changed

+386
-0
lines changed

002._add_two_numbers.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
###2. Add Two Numbers
2+
3+
题目:
4+
<https://leetcode.com/problems/add-two-numbers/>
5+
6+
7+
难度 : Medium
8+
9+
10+
跟plus One, add Binary 玩的同一种花样
11+
12+
13+
```
14+
class Solution(object):
15+
def addTwoNumbers(self, l1, l2):
16+
"""
17+
:type l1: ListNode
18+
:type l2: ListNode
19+
:rtype: ListNode
20+
"""
21+
#easiest case
22+
if l1 == None:
23+
return l2
24+
if l2 == None:
25+
return l1
26+
27+
if l1.val + l2.val < 10:
28+
l3 = ListNode(l1.val + l2.val)
29+
l3.next = self.addTwoNumbers(l1.next, l2.next)
30+
31+
elif l1.val + l2.val >= 10:
32+
l3 = ListNode(l1.val + l2.val - 10)
33+
tmp = ListNode(1)
34+
tmp.next = None
35+
l3.next = self.addTwoNumbers(l1.next, self.addTwoNumbers(l2.next ,tmp))
36+
return l3
37+
```

024._swap_nodes_in_pairs.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
###24. Swap Nodes in Pairs
2+
3+
题目:
4+
<https://leetcode.com/problems/swap-nodes-in-pairs/>
5+
6+
7+
难度 : Easy
8+
9+
看了hint,用loop做,每个node关系要弄清楚
10+
11+
12+
```
13+
14+
class Solution(object):
15+
def swapPairs(self, head):
16+
"""
17+
:type head: ListNode
18+
:rtype: ListNode
19+
"""
20+
if head == None or head.next == None:
21+
return head
22+
23+
dummy = ListNode(-1)
24+
dummy.next = head
25+
26+
cur = dummy
27+
28+
while cur.next and cur.next.next:
29+
next_one, next_two, next_three = cur.next, cur.next.next, cur.next.next.next
30+
cur.next = next_two
31+
next_two.next = next_one
32+
next_one.next = next_three
33+
cur = next_one
34+
return dummy.next
35+
```

049._group_anagrams_python.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
###49. Group Anagrams python
2+
3+
题目:
4+
<https://leetcode.com/problems/anagrams/>
5+
6+
7+
难度 : Medium
8+
9+
我又来使用我的取巧神奇python大法
10+
11+
12+
```
13+
class Solution(object):
14+
def groupAnagrams(self, strs):
15+
"""
16+
:type strs: List[str]
17+
:rtype: List[List[str]]
18+
"""
19+
mapx = {}
20+
for str1 in strs:
21+
key = self.sortedWord(str1)
22+
if key in mapx:
23+
mapx[key].append(str1)
24+
else:
25+
mapx[key] = [str1]
26+
return list(mapx.values())
27+
28+
def sortedWord(self,s):
29+
"""
30+
:type s: str
31+
:type t: str
32+
:rtype: bool
33+
"""
34+
sList = sorted(list(s))
35+
str1 = ''.join(sList)
36+
return str1
37+
38+
```

066._plus_one.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
###66. Plus One
2+
3+
题目:
4+
<https://leetcode.com/problems/plus-one/>
5+
6+
7+
难度 : Easy
8+
9+
10+
11+
12+
奇怪的AC了
13+
😄
14+
搞笑
15+
16+
17+
```
18+
19+
class Solution(object):
20+
def plusOne(self, digits):
21+
"""
22+
:type digits: List[int]
23+
:rtype: List[int]
24+
"""
25+
if digits == []:
26+
return [1]
27+
if digits[-1] < 9:
28+
return digits[:-1] + [digits[-1] + 1]
29+
else:
30+
return self.plusOne(digits[:-1]) + [0]
31+
```

067._add_binary.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
###67. Add Binary
2+
3+
题目:
4+
<https://leetcode.com/problems/add-binary/>
5+
6+
7+
难度 : Easy
8+
9+
10+
几种case:
11+
12+
- a or b 为空,最简单
13+
- 唯一的问题是如果有进位的处理,进位的处理就是先让其中的一个数addBinary +1 ,然后再用addBinary
14+
15+
```
16+
17+
18+
class Solution(object):
19+
def addBinary(self, a, b):
20+
"""
21+
:type a: str
22+
:type b: str
23+
:rtype: str
24+
"""
25+
if (a == '' or b == ''):
26+
return a + b
27+
elif a[-1] == '0' and b[-1] == '0':
28+
return self.addBinary(a[:-1], b[:-1]) + '0'
29+
elif a[-1] == '1' and b[-1] == '1':
30+
return self.addBinary(a[:-1], self.addBinary(b[:-1],'1')) + '0'
31+
else:
32+
return self.addBinary(a[:-1], b[:-1]) + '1'
33+
```

223._rectangle_area.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
###223. Rectangle Area
2+
3+
题目:
4+
<https://leetcode.com/problems/add-two-numbers/>
5+
6+
7+
难度 : Easy
8+
9+
10+
这道题是我瞎了狗眼,🐶,之前看错了,以为要求相交的部分,结果是求cover的部分,所以写的长||||||
11+
12+
13+
```
14+
class Solution(object):
15+
def computeArea(self, A, B, C, D, E, F, G, H):
16+
"""
17+
:type A: int
18+
:type B: int
19+
:type C: int
20+
:type D: int
21+
:type E: int
22+
:type F: int
23+
:type G: int
24+
:type H: int
25+
:rtype: int
26+
"""
27+
return self.area(C - A, D - B) + self.area(H - F, G - E ) - self.area(self.interSect(A,C,E,G), self.interSect(B,D,F,H))
28+
29+
def area(self, w, h):
30+
if w * h < 0:
31+
return - w * h
32+
return w * h
33+
34+
35+
def interSect(self, A, C, E, G):
36+
if E > C:
37+
return 0
38+
elif G < A:
39+
return 0
40+
elif E >= A and G <= C:
41+
return G - E
42+
elif A >= E and C <= G:
43+
return C - A
44+
elif G <= C and G >= A and E <= A:
45+
return G - A
46+
else:
47+
return C - E
48+
49+
50+
```
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
###235. Lowest Common Ancestor of a Binary Search Tree
2+
3+
题目:
4+
<https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/>
5+
6+
7+
难度 : Easy
8+
9+
- 两个node,一个大于root,一个小于root,那么必定root两边,共同的ancestor是root,同时再考虑同为空的状况
10+
- 两个node,都比node小,到左边去寻找,那么先找到那个必定是common ancestor
11+
- 两个node,都比node大,类似....
12+
13+
14+
AC解法
15+
16+
```
17+
class Solution(object):
18+
def lowestCommonAncestor(self, root, p, q):
19+
"""
20+
:type root: TreeNode
21+
:type p: TreeNode
22+
:type q: TreeNode
23+
:rtype: TreeNode
24+
"""
25+
if root == None or root == p or root == q:
26+
return root
27+
elif p.val < root.val < q.val or q.val < root.val < p.val :
28+
return root
29+
elif p.val < root.val and q.val < root.val:
30+
return self.lowestCommonAncestor(root.left,p,q)
31+
else:
32+
return self.lowestCommonAncestor(root.right,p,q)
33+
```

242._valid_anagram.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,31 @@ class Solution(object):
2424
2525
```
2626

27+
28+
作弊神奇python大法,看了看别人的解法,用字数统计,因为只可能是26个字母
29+
30+
然后发现作弊大法居然更快
31+
32+
```
33+
34+
class Solution(object):
35+
def isAnagram(self, s, t):
36+
"""
37+
:type s: str
38+
:type t: str
39+
:rtype: bool
40+
"""
41+
if len(s) != len(t):
42+
return False
43+
44+
charCnt = [0] * 26
45+
46+
for i in range(len(s)):
47+
charCnt[ord(s[i]) - 97] += 1
48+
49+
for i in range(len(t)):
50+
charCnt[ord(t[i]) - 97] -= 1
51+
if charCnt[ord(t[i]) - 97] < 0:
52+
return False
53+
return True
54+
```

292._nim_game.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
###292. Nim Game
2+
3+
题目:
4+
5+
<https://leetcode.com/problems/nim-game/>
6+
7+
8+
难度:
9+
10+
Easy
11+
12+
13+
对于总是优先开始的那方
14+
15+
16+
- 有一到三块,总是赢
17+
- 有四块,总是输
18+
- 有五块,总是赢
19+
20+
所以如果自己想赢,总是要迫使对方拿之后,给自己遗留5块,或者三块以及以下。
21+
22+
- 如果是六块:
23+
- 拿一块,对方五块,对方赢
24+
- 拿两块,对方余下四块,我方赢
25+
- 拿三块,余三块,对方赢
26+
27+
- 七块:
28+
- 拿三块,余四块,迫使对方输,总是赢
29+
30+
本打算用递归来看,因为对方也可以重复使用这个函数,但是会超时,所以就看了一下hint
31+
32+
33+
- n <= 3 能赢 √
34+
- n == 4 总输
35+
- n = 5,6,7 总赢
36+
- n == 8, 先手如何选,总可以转成5,6,7 对方总会赢
37+
38+
39+
所以 n % 4 != 0 时候,先手必胜
40+
41+
简直是啊,有些游戏就是这样来必赢的啊,没想到你是这样的题目
42+
43+
44+
45+
```
46+
class Solution(object):
47+
def canWinNim(self, n):
48+
"""
49+
:type n: int
50+
:rtype: bool
51+
"""
52+
return n % 4 != 0
53+
```
54+
55+

0 commit comments

Comments
 (0)