Skip to content

Commit 6368cd8

Browse files
committed
20190604_150
1 parent f1df81c commit 6368cd8

8 files changed

+235
-42
lines changed

Python/copy-list-with-random-pointer.py

+45-18
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ class Solution:
3434
def copyRandomList(self, head: 'Node') -> 'Node':
3535

3636
# Approach one 深拷贝不是引用,需要新构造一个链表
37-
# 构造新链表
38-
# 赋值 random 指针
37+
# 构造新链表赋值 random 指针
3938

4039
# pre = pre2 = Node(0, None, None)
4140
# orig = orig1 = head
@@ -61,20 +60,48 @@ def copyRandomList(self, head: 'Node') -> 'Node':
6160

6261
# Approach two O(n) O(1)
6362

63+
# if not head: return None
64+
# pre = None
65+
# current = head
66+
67+
# while current:
68+
# current.random = Node(current.val, None, current.random)
69+
# if pre:
70+
# pre.random.next = current.random
71+
# pre = current
72+
# current = current.next
73+
74+
# current = head.random
75+
# while current:
76+
# if current.random:
77+
# current.random = current.random.random
78+
# current = current.next
79+
# return head.random
80+
81+
82+
83+
# Approach three
6484
if not head: return None
65-
pre = None
66-
current = head
67-
68-
while current:
69-
current.random = Node(current.val, None, current.random)
70-
if pre:
71-
pre.random.next = current.random
72-
pre = current
73-
current = current.next
74-
75-
current = head.random
76-
while current:
77-
if current.random:
78-
current.random = current.random.random
79-
current = current.next
80-
return head.random
85+
86+
# 将新结点复制出来,插进原链表
87+
tmp = head
88+
while tmp:
89+
node = Node(tmp.val,tmp.next,None)
90+
tmp.next, tmp = node , node.next
91+
92+
# 复制随机指针
93+
tmp = head
94+
while tmp:
95+
if tmp.random:
96+
tmp.next.random = tmp.random.next
97+
tmp = tmp.next.next
98+
99+
# 拆分两个链表(注意边界条件,插入后的链表一个为偶数,原链表结点为空的时候就可以停止)
100+
res = new_head = Node(0, None, None)
101+
origin = head
102+
while origin:
103+
new_head.next = origin.next
104+
new_head = new_head.next
105+
origin.next = new_head.next
106+
origin = origin.next
107+
return res.next

Python/count-primes.py

+10-18
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,13 @@
99
'''
1010

1111

12-
1312
class Solution:
14-
def countPrimes(self, n):
15-
"""
16-
:type n: int
17-
:rtype: int
18-
"""
13+
def countPrimes(self, n: int) -> int:
1914
# 思路一: 简单的遍历除以小于他的数,效率太低了!
2015
# 思路二: 遍历除以小于他一半的数,有率提升一倍
21-
# 思路三: 动态简历素数表,这样居然还不行?!效率还是低
16+
# 思路三: 动态建立素数表,这样居然还不行?!效率还是低
2217
# num = 0
23-
# result = [2 ]
18+
# result = [2 ]
2419
# for i in range(2, n):
2520
# for j in range(len(result)):
2621
# if i % result[j] == 0 and i!=result[j]:
@@ -31,16 +26,13 @@ def countPrimes(self, n):
3126
# return num
3227

3328
# 思路四: Sieve of Eratosthenes(埃拉托色尼筛法,简称埃氏筛法),参考维基百科:https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
34-
# fasle 表示不是素数, true 代表素数. 所以总和是多少,最后就有多少个.
35-
36-
37-
# 注意审题边界条件,是小于n,不包括n
38-
if n < 2:
39-
return 0 # 注意 数组越界的情况
29+
# 0 表示不是素数, 1 代表素数. 所以总和是多少,最后就有多少个
30+
if n < 3: # 注意审题边界条件,是小于n,不包括n
31+
return 0 # 注意 数组越界的情况
4032

41-
primes = [True] * n
42-
primes[0] = primes[1] = False
43-
for i in range(2, int(n**0.5)+1): # 在选择除数时候的一个小技巧.大于一半的数是不可能做除数的. 事实证明, 能够节省400ms的时间
33+
primes = [1] * n
34+
primes[0] = primes[1] = 0
35+
for i in range(2, int(n**0.5)+1): # 在选择除数时候的一个小技巧.大于一半的数是不可能做除数的
4436
if primes[i]:
45-
primes[i * i: n: i] = [False] * len(primes[i * i: n: i]) # 非常简洁的语句, 从小到大用埃氏筛法, 将每一个不是素数的数筛选掉.大大减少出发的数量.
37+
primes[i * i: n: i] = [0] * ((n - 1) // i - i + 1) # 用埃氏筛法, 将每一个不是素数的数筛选掉.大大减少除法的数量.
4638
return sum(primes)

Python/excel-sheet-column-number.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'''
2+
3+
Given a column title as appear in an Excel sheet, return its corresponding column number.
4+
5+
For example:
6+
7+
A -> 1
8+
B -> 2
9+
C -> 3
10+
...
11+
Z -> 26
12+
AA -> 27
13+
AB -> 28
14+
...
15+
Example 1:
16+
17+
Input: "A"
18+
Output: 1
19+
Example 2:
20+
21+
Input: "AB"
22+
Output: 28
23+
Example 3:
24+
25+
Input: "ZY"
26+
Output: 701
27+
28+
'''
29+
30+
class Solution:
31+
def titleToNumber(self, s: str) -> int:
32+
res = 0
33+
for i in s:
34+
res = res*26 + ord(i) - 64
35+
return res

Python/factorial-trailing-zeroes.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'''
2+
3+
Given an integer n, return the number of trailing zeroes in n!.
4+
5+
Example 1:
6+
7+
Input: 3
8+
Output: 0
9+
Explanation: 3! = 6, no trailing zero.
10+
Example 2:
11+
12+
Input: 5
13+
Output: 1
14+
Explanation: 5! = 120, one trailing zero.
15+
Note: Your solution should be in logarithmic time complexity.
16+
17+
18+
'''
19+
20+
21+
22+
class Solution:
23+
def trailingZeroes(self, n: int) -> int:
24+
# 将原问题简化为寻找有多少个2*5的个数,只有这样才能出现零。因为2必然比5多,只关注5就好。
25+
res = 0
26+
while n >= 5:
27+
n = n // 5
28+
res += n
29+
return res

Python/house-robber-ii.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'''
2+
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
3+
4+
Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.
5+
6+
Example 1:
7+
8+
Input: [2,3,2]
9+
Output: 3
10+
Explanation: You cannot rob house 1 (money = 2) and then rob house 3 (money = 2),
11+
because they are adjacent houses.
12+
Example 2:
13+
14+
Input: [1,2,3,1]
15+
Output: 4
16+
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
17+
Total amount you can rob = 1 + 3 = 4.
18+
19+
'''
20+
21+
22+
23+
class Solution:
24+
def rob(self, nums: List[int]) -> int:
25+
if not nums: return 0
26+
def judge_rob(numbers):
27+
pre , cur = 0, 0
28+
for i in numbers:
29+
pre , cur = cur , max(pre+i , cur)
30+
return cur
31+
return max(judge_rob(nums[1:]), judge_rob(nums[:-1])) if len(nums) > 1 else nums[0]

Python/house-robber-iii.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
'''
2+
The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root, each house has one and only one parent house. After a tour, the smart thief realized that "all houses in this place forms a binary tree". It will automatically contact the police if two directly-linked houses were broken into on the same night.
3+
4+
Determine the maximum amount of money the thief can rob tonight without alerting the police.
5+
6+
Example 1:
7+
8+
Input: [3,2,3,null,3,null,1]
9+
10+
3
11+
/ \
12+
2 3
13+
\ \
14+
3 1
15+
16+
Output: 7
17+
Explanation: Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.
18+
Example 2:
19+
20+
Input: [3,4,5,1,3,null,1]
21+
22+
3
23+
/ \
24+
4 5
25+
/ \ \
26+
1 3 1
27+
28+
Output: 9
29+
Explanation: Maximum amount of money the thief can rob = 4 + 5 = 9.
30+
'''
31+
32+
33+
34+
# Definition for a binary tree node.
35+
# class TreeNode:
36+
# def __init__(self, x):
37+
# self.val = x
38+
# self.left = None
39+
# self.right = None
40+
41+
class Solution:
42+
def rob(self, root: TreeNode) -> int:
43+
def robHelper(root):
44+
if not root:
45+
return (0, 0)
46+
left, right = robHelper(root.left), robHelper(root.right)
47+
return (root.val + left[1] + right[1], max(left) + max(right))
48+
49+
return max(robHelper(root))

Python/house-robber.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'''
2+
3+
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
4+
5+
Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.
6+
7+
Example 1:
8+
9+
Input: [1,2,3,1]
10+
Output: 4
11+
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
12+
Total amount you can rob = 1 + 3 = 4.
13+
Example 2:
14+
15+
Input: [2,7,9,3,1]
16+
Output: 12
17+
Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1).
18+
Total amount you can rob = 2 + 9 + 1 = 12.
19+
20+
21+
'''
22+
23+
24+
25+
class Solution:
26+
def rob(self, nums: List[int]) -> int:
27+
pre , cur = 0,0
28+
for i in nums:
29+
pre , cur = cur , max(pre + i , cur)
30+
return cur

README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ Binary Search Manipulation](https://github.com/OnlyChristmas/leetcode#bit-manipu
278278
| 082 | [Remove Duplicates from Sorted List II](https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/) | [Python](./Python/remove-duplicates-from-sorted-list-ii.py) | O(n) | O(1) | Medium |
279279
| 083 | [Remove Duplicates from Sorted List](https://leetcode.com/problems/remove-duplicates-from-sorted-list/) | [Python](./Python/remove-duplicates-from-sorted-list.py) | O(n) | O(1) | Easy |
280280
| 092 | [Reverse Linked List II](https://leetcode.com/problems/reverse-linked-list-ii/) | [Python](./Python/reverse-linked-list-ii.py) | | | Medium |
281-
| 138 | [Copy List with Random Pointer](https://leetcode.com/problems/copy-list-with-random-pointer/) | [Python](./Python/copy-list-with-random-pointer.py) | O(n) | O(n) | Hard |
281+
| 138 | [Copy List with Random Pointer](https://leetcode.com/problems/copy-list-with-random-pointer/) | [Python](./Python/copy-list-with-random-pointer.py) | O(n) | O(1) | Medium |
282282
| 160 | [Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists/) | [Python](./Python/intersection-of-two-linked-lists.py) | O(n) | O(1) | Easy |
283283
| 203 | [Remove Linked List Elements](https://leetcode.com/problems/remove-linked-list-elements/) | [Python](./Python/remove-linked-list-elements.py) | O(n) | O(1) | Easy |
284284
| 206 | [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/) | [Python](./Python/reverse-linked-list.py) | O(n) | O(1) | Easy |
@@ -495,8 +495,8 @@ Binary Search Manipulation](https://github.com/OnlyChristmas/leetcode#bit-manipu
495495
| 089 | [Gray Code](https://leetcode.com/problems/gray-code/) | [Python](./Python/gray-code.py) | | | Medium |
496496
| 166 | [Fraction to Recurring Decimal](https://leetcode.com/problems/fraction-to-recurring-decimal/) | [Python](./Python/fraction-to-recurring-decimal.py) | | | Medium |
497497
| 168 | [Excel Sheet Column Title](https://leetcode.com/problems/excel-sheet-column-title/) | [Python](./Python/excel-sheet-column-title.py) | | | Easy |
498-
| 171 | [Excel Sheet Column Number](https://leetcode.com/problems/excel-sheet-column-number/) | [Python](./Python/excel-sheet-column-number.py) | | | Easy |
499-
| 172 | [Factorial Trailing Zeroes](https://leetcode.com/problems/factorial-trailing-zeroes/) | [Python](./Python/factorial-trailing-zeroes.py) | | | Easy |
498+
| 171 | [Excel Sheet Column Number](https://leetcode.com/problems/excel-sheet-column-number/) | [Python](./Python/excel-sheet-column-number.py) | O(n) | O(1) | Easy |
499+
| 172 | [Factorial Trailing Zeroes](https://leetcode.com/problems/factorial-trailing-zeroes/) | [Python](./Python/factorial-trailing-zeroes.py) | O(1) | O(1) | Easy |
500500
| 202 | [Happy Number](https://leetcode.com/problems/happy-number/) | [Python](./Python/happy-number.py) | O(n) | O(1) | Easy |
501501
| 204 | [Count Primes](https://leetcode.com/problems/count-primes/) | [Python](./Python/count-primes.py) | O(n) | O(n) | Easy |
502502
| 223 | [Rectangle Area](https://leetcode.com/problems/rectangle-area/) | [Python](./Python/rectangle-area.py) | | | Easy |
@@ -638,7 +638,7 @@ Binary Search Manipulation](https://github.com/OnlyChristmas/leetcode#bit-manipu
638638
| 298 | [Binary Tree Longest Consecutive Sequence](https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/) | [Python](./Python/binary-tree-longest-consecutive-sequence.py) | O(n) | O(1) | Medium |
639639
| 327 | [Count of Range Sum](https://leetcode.com/problems/count-of-range-sum/) | [Python](./Python/count-of-range-sum.py) | | | Hard |
640640
| 333 | [Largest BST Subtree](https://leetcode.com/problems/largest-bst-subtree/) | [Python](./Python/largest-bst-subtree.py) | | | Medium |
641-
| 337 | [House Robber III](https://leetcode.com/problems/house-robber-iii/) | [Python](./Python/house-robber-iii.py) | | | Medium |
641+
| 337 | [House Robber III](https://leetcode.com/problems/house-robber-iii/) | [Python](./Python/house-robber-iii.py) | O(logn) | O(1) | Medium |
642642
| 395 | [Longest Substring with At Least K Repeating Characters](https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/) | [Python](./Python/longest-substring-with-at-least-k-repeating-characters.py) | | | Medium |
643643
| 404 | [Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves/) | [Python](./Python/sum-of-left-leaves.py) | | | Easy |
644644
| 437 | [Path Sum III](https://leetcode.com/problems/path-sum-iii/) | [Python](./Python/path-sum-iii.py) | | | Easy |
@@ -842,8 +842,8 @@ Binary Search Manipulation](https://github.com/OnlyChristmas/leetcode#bit-manipu
842842
| 152 | [Maximum Product Subarray](https://leetcode.com/problems/maximum-product-subarray/) | [Python](./Python/maximum-product-subarray.py) | | | Medium |
843843
| 174 | [Dungeon Game](https://leetcode.com/problems/dungeon-game/) | [Python](./Python/dungeon-game.py) | | | Hard |
844844
| 188 | [Best Time to Buy and Sell Stock IV](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/) | [Python](./Python/best-time-to-buy-and-sell-stock-iv.py) | | | Hard |
845-
| 198 | [House Robber](https://leetcode.com/problems/house-robber/) | [Python](./Python/house-robber.py) | | | Easy |
846-
| 213 | [House Robber II](https://leetcode.com/problems/house-robber-ii/) | [Python](./Python/house-robber-ii.py) | | | Medium |
845+
| 198 | [House Robber](https://leetcode.com/problems/house-robber/) | [Python](./Python/house-robber.py) | O(n) | O(1) | Easy |
846+
| 213 | [House Robber II](https://leetcode.com/problems/house-robber-ii/) | [Python](./Python/house-robber-ii.py) | O(n) | O(1) | Medium |
847847
| 221 | [Maximal Square](https://leetcode.com/problems/maximal-square/) | [Python](./Python/maximal-square.py) | | | Medium |
848848
| 256 | [Paint House](https://leetcode.com/problems/paint-house/) | [Python](./Python/paint-house.py) | | | Medium |
849849
| 265 | [Paint House II](https://leetcode.com/problems/paint-house-ii/) | [Python](./Python/paint-house-ii.py) | | | Hard |

0 commit comments

Comments
 (0)