Skip to content

Commit dc6a2ea

Browse files
author
Jack
committed
add some solutions
1 parent 00d783e commit dc6a2ea

5 files changed

+77
-8
lines changed

Diff for: Python/implement-strstr.py

+11
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@ def getPrefix(self, pattern):
4343
j += 1
4444
prefix[i] = j
4545
return prefix
46+
47+
def strStr2(self, haystack, needle):
48+
"""
49+
:type haystack: str
50+
:type needle: str
51+
:rtype: int
52+
"""
53+
try:
54+
return haystack.index(needle)
55+
except:
56+
return -1
4657

4758
# Time: O(n * k)
4859
# Space: O(k)

Diff for: Python/jump-game-ii.py

+27
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# The minimum number of jumps to reach the last index is 2. (Jump 1 step from index 0 to 1, then 3 steps to the last index.)
1414
#
1515

16+
# not pass on leetcode because of time limit
1617
class Solution:
1718
# @param A, a list of integers
1819
# @return an integer
@@ -29,6 +30,7 @@ def jump(self, A):
2930
reachable = max(reachable, i + length)
3031
return jump_count
3132

33+
# not pass on leetcode because of time limit
3234
# Time: O(n^2)
3335
# Space: O(1)
3436
class Solution2:
@@ -44,6 +46,31 @@ def jump(self, A):
4446
for i, length in enumerate(A[:reachable + 1]):
4547
reachable = max(reachable, i + length)
4648
return -1
49+
50+
# when you on an index of nums, move to next index which can move farthest in range of this index reachable
51+
# Time: O(log(n))
52+
# Space: O(1)
53+
class Solution3(object):
54+
def jump(self, nums):
55+
"""
56+
:type nums: List[int]
57+
:rtype: int
58+
"""
59+
nums2, l = [i + j for i, j in enumerate(nums)], len(nums) - 1
60+
if not l: return 0
61+
62+
def find_max_index(index):
63+
if index + nums[index] >= l: return l
64+
tmp = nums2[index + 1:index + nums[index] + 1]
65+
return index + tmp.index(max(tmp)) + 1
66+
67+
index, steps = 0, 0
68+
while True:
69+
index = find_max_index(index)
70+
steps += 1
71+
if index == l:
72+
break
73+
return steps
4774

4875
if __name__ == "__main__":
4976
print Solution().jump([2,3,1,1,4])

Diff for: Python/majority-element-ii.py

+9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
# Given an integer array of size n,
55
# find all elements that appear more than [n/3] times.
66
# The algorithm should run in linear time and in O(1) space.
7+
import collections
8+
79

810
class Solution(object):
911
def majorityElement(self, nums):
@@ -41,3 +43,10 @@ def majorityElement(self, nums):
4143
result.append(i)
4244

4345
return result
46+
47+
def majorityElement2(self, nums):
48+
"""
49+
:type nums: List[int]
50+
:rtype: List[int]
51+
"""
52+
return [i[0] for i in collections.Counter(nums).items() if i[1] > len(nums) / 3]

Diff for: Python/majority-element.py

+17-7
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,34 @@
55
# The majority element is the element that appears more than [n/2] times.
66
#
77
# You may assume that the array is non-empty and the majority element always exist in the array.
8-
#
8+
import collections
9+
910

1011
class Solution:
11-
# @param num, a list of integers
12-
# @return an integer
13-
def majorityElement(self, num):
12+
def majorityElement(self, nums):
13+
"""
14+
:type nums: List[int]
15+
:rtype: int
16+
"""
1417
idx, cnt = 0, 1
1518

16-
for i in xrange(1, len(num)):
17-
if num[idx] == num[i]:
19+
for i in xrange(1, len(nums)):
20+
if nums[idx] == nums[i]:
1821
cnt += 1
1922
else:
2023
cnt -= 1
2124
if cnt == 0:
2225
idx = i
2326
cnt = 1
2427

25-
return num[idx]
28+
return nums[idx]
29+
30+
def majorityElement2(self, nums):
31+
"""
32+
:type nums: List[int]
33+
:rtype: int
34+
"""
35+
return sorted(collections.Counter(nums).items(), key=lambda a: a[1], reverse=True)[0][0]
2636

2737
if __name__ == "__main__":
2838
print Solution().majorityElement([1, 2, 3, 4, 5, 5, 5, 5, 5, 5, 6])

Diff for: Python/repeated-dna-sequences.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
#
1313
# Return:
1414
# ["AAAAACCCCC", "CCCCCAAAAA"].
15-
#
15+
import collections
16+
1617

1718
class Solution:
1819
# @param s, a string
@@ -29,6 +30,17 @@ def findRepeatedDnaSequences(self, s):
2930
dict[rolling_hash] = False
3031
return res
3132

33+
def findRepeatedDnaSequences2(self, s):
34+
"""
35+
:type s: str
36+
:rtype: List[str]
37+
"""
38+
l, r = [], []
39+
if len(s) < 10: return []
40+
for i in range(len(s) - 9):
41+
l.extend([s[i:i + 10]])
42+
return [k for k, v in collections.Counter(l).items() if v > 1]
43+
3244
if __name__ == "__main__":
3345
print Solution().findRepeatedDnaSequences("AAAAAAAAAA")
3446
print Solution().findRepeatedDnaSequences("")

0 commit comments

Comments
 (0)