Skip to content

Commit b3583c1

Browse files
committed
update bunch
1 parent 708c190 commit b3583c1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+1603
-92
lines changed

array/house_robber.py

Lines changed: 0 additions & 11 deletions
This file was deleted.

array/plus_one.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# The digits are stored such that the most significant
55
# digit is at the head of the list.
66

7+
78
def plusOne(digits):
89
"""
910
:type digits: List[int]

array/rotate_array.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""
2+
Rotate an array of n elements to the right by k steps.
3+
4+
For example, with n = 7 and k = 3,
5+
the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].
6+
7+
Note:
8+
Try to come up as many solutions as you can,
9+
there are at least 3 different ways to solve this problem.
10+
"""
11+
12+
13+
def rotate(nums, k):
14+
"""
15+
:type nums: List[int]
16+
:type k: int
17+
:rtype: void Do not return anything, modify nums in-place instead.
18+
"""
19+
n = len(nums)
20+
k = k % n
21+
reverse(nums, 0, n - k - 1)
22+
reverse(nums, n - k, n - 1)
23+
reverse(nums, 0, n - 1)
24+
25+
26+
def reverse(array, a, b):
27+
while a < b:
28+
array[a], array[b] = array[b], array[a]
29+
a += 1
30+
b -= 1

array/three_sum.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""
2+
Given an array S of n integers, are there elements a, b, c in S
3+
such that a + b + c = 0?
4+
Find all unique triplets in the array which gives the sum of zero.
5+
6+
Note: The solution set must not contain duplicate triplets.
7+
8+
For example, given array S = [-1, 0, 1, 2, -1, -4],
9+
10+
A solution set is:
11+
[
12+
[-1, 0, 1],
13+
[-1, -1, 2]
14+
]
15+
"""
16+
17+
18+
def three_sum(nums:"List[int]")->"List[int]":
19+
res = []
20+
nums.sort()
21+
for i in range(len(nums)-2):
22+
if i > 0 and nums[i] == nums[i-1]:
23+
continue
24+
l, r = i+1, len(nums)-1
25+
while l < r:
26+
s = nums[i] + nums[l] + nums[r]
27+
if s > 0:
28+
r -= 1
29+
elif s < 0:
30+
l += 1
31+
else:
32+
# found three sum
33+
res.append((nums[i], nums[l], nums[r]))
34+
# remove duplicates
35+
while l < r and nums[l] == nums[l+1]:
36+
l+=1
37+
while l < r and nums[r] == nums[r-1]:
38+
r -= 1
39+
l += 1
40+
r -= 1
41+
return res
42+
43+
44+
if __name__ == "__main__":
45+
x = [-1,0,1,2,-1,-4]
46+
print(three_sum(x))

array/two_sum.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
Given an array of integers, return indices of the two numbers
3+
such that they add up to a specific target.
4+
5+
You may assume that each input would have exactly one solution,
6+
and you may not use the same element twice.
7+
8+
Example:
9+
Given nums = [2, 7, 11, 15], target = 9,
10+
11+
Because nums[0] + nums[1] = 2 + 7 = 9,
12+
return [0, 1].
13+
"""
14+
15+
16+
def two_sum(nums:"List[int]", target:"int")->"List[int]":
17+
dic = {}
18+
for i, num in enumerate(nums):
19+
if num in dic:
20+
return [dic[num], i]
21+
else:
22+
dic[target - num] = i
23+
24+
25+
if __name__ == "__main__":
26+
arr = [3,2,4]
27+
target = 6
28+
res = two_sum(arr, target)
29+
print(res)

backtrack/generate_parenthesis.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""
2+
Given n pairs of parentheses, write a function to generate
3+
all combinations of well-formed parentheses.
4+
5+
For example, given n = 3, a solution set is:
6+
7+
[
8+
"((()))",
9+
"(()())",
10+
"(())()",
11+
"()(())",
12+
"()()()"
13+
]
14+
"""
15+
16+
def gen_parenthesis(n:"int")->"List[str]":
17+
res = []
18+
add_pair(res, "", n, 0)
19+
return res
20+
21+
def add_pair(res, s, left, right):
22+
if left == 0 and right == 0:
23+
res.append(s)
24+
return
25+
if right > 0:
26+
add_pair(res, s+")", left, right-1)
27+
if left > 0:
28+
add_pair(res, s+"(", left-1, right+1)
29+
30+
31+
if __name__=="__main__":
32+
print(gen_parenthesis(3))

backtrack/letter_combination.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""
2+
Given a digit string, return all possible letter
3+
combinations that the number could represent.
4+
5+
Input:Digit string "23"
6+
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
7+
"""
8+
9+
10+
def letter_combinations(digits:"str")->"List[str]":
11+
if digits == "":
12+
return []
13+
kmaps = {
14+
"2": "abc",
15+
"3": "def",
16+
"4": "ghi",
17+
"5": "jkl",
18+
"6": "mno",
19+
"7": "pqrs",
20+
"8": "tuv",
21+
"9": "wxyz"
22+
}
23+
ans = [""]
24+
for num in digits:
25+
tmp = []
26+
for an in ans:
27+
for char in kmaps[num]:
28+
tmp.append(an + char)
29+
ans = tmp
30+
return ans
31+
32+
33+
if __name__ == "__main__":
34+
digit_string = "23"
35+
print(letter_combinations(digit_string))

backtrack/subsets.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
# ]
1818

1919

20+
# O(2**n)
21+
22+
2023
def subsets(nums):
2124
res = []
2225
backtrack(res, nums, [], 0)
@@ -44,6 +47,13 @@ def backtrack(res, nums, stack, pos):
4447
# backtrack(res, nums, cur, pos+1)
4548

4649

50+
# Iteratively
51+
def subsets2(self, nums):
52+
res = [[]]
53+
for num in sorted(nums):
54+
res += [item+[num] for item in res]
55+
return res
56+
4757
test = [1,2,3]
4858
print(test)
4959
print(subsets(test))

bit/count_ones.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""
2+
Write a function that takes an unsigned integer and
3+
returns the number of ’1' bits it has
4+
(also known as the Hamming weight).
5+
6+
For example, the 32-bit integer ’11' has binary
7+
representation 00000000000000000000000000001011,
8+
so the function should return 3.
9+
"""
10+
11+
12+
def count_ones(n):
13+
"""
14+
:type n: int
15+
:rtype: int
16+
"""
17+
counter = 0
18+
while n:
19+
counter += n & 1
20+
n >>= 1
21+
return counter

bit/reverse_bits.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""
2+
Reverse bits of a given 32 bits unsigned integer.
3+
4+
For example, given input 43261596
5+
(represented in binary as 00000010100101000001111010011100),
6+
return 964176192
7+
(represented in binary as 00111001011110000010100101000000).
8+
"""
9+
10+
11+
def reverse_bits(n):
12+
m = 0
13+
i = 0
14+
while i < 32:
15+
m = (m << 1) + (n & 1)
16+
n >>= 1
17+
i += 1
18+
return m

bit/single_number.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""
2+
Given an array of integers, every element appears
3+
twice except for one. Find that single one.
4+
5+
Note:
6+
Your algorithm should have a linear runtime complexity.
7+
Could you implement it without using extra memory?
8+
"""
9+
10+
11+
def single_number(nums):
12+
"""
13+
:type nums: List[int]
14+
:rtype: int
15+
"""
16+
i = 0
17+
for num in nums:
18+
i ^= num
19+
return i

bit/single_number2.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""
2+
Given an array of integers, every element appears
3+
three times except for one, which appears exactly once.
4+
Find that single one.
5+
6+
Note:
7+
Your algorithm should have a linear runtime complexity.
8+
Could you implement it without using extra memory?
9+
"""
10+
11+
12+
"""
13+
32 bits for each integer.
14+
Consider 1 bit in it, the sum of each integer's corresponding bit
15+
(except for the single number)
16+
should be 0 if mod by 3. Hence, we sum the bits of all
17+
integers and mod by 3,
18+
the remaining should be the exact bit of the single number.
19+
In this way, you get the 32 bits of the single number.
20+
"""
21+
22+
23+
def single_number(nums):
24+
"""
25+
:type nums: List[int]
26+
:rtype: int
27+
"""
28+
res = 0
29+
for i in range(0, 32):
30+
count = 0
31+
for num in nums:
32+
if ((num >> i) & 1):
33+
count += 1
34+
res |= ((count % 3) << i)
35+
if res >= 2**31:
36+
res -= 2**32
37+
return res
38+
39+
40+
# Another awesome answer
41+
def single_number2(nums):
42+
ones, twos = 0, 0
43+
for i in range(len(nums)):
44+
ones = (ones ^ nums[i]) & ~twos
45+
twos = (twos ^ nums[i]) & ~ones
46+
return ones

0 commit comments

Comments
 (0)