Skip to content

Commit 1e08f64

Browse files
committed
add 26 784 KMP
1 parent b25c4ca commit 1e08f64

12 files changed

+191
-0
lines changed

118.pascals-triangle.py

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
# -*- coding: utf-8 -*-
2+
# @Time : 2019/2/25 11:46
3+
# @Author : xulzee
4+
5+
# @File : 118.pascals-triangle.py
6+
# @Software: PyCharm
7+
8+
19
class Solution:
210
def generate(self, numRows):
311
"""
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# -*- coding: utf-8 -*-
2+
# @Time : 2019/2/26 14:56
3+
# @Author : xulzee
4+
5+
# @File : 122. Best Time to Buy and Sell Stock II.py
6+
# @Software: PyCharm
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# -*- coding: utf-8 -*-
2+
# @Time : 2019/2/26 11:47
3+
# @Author : xulzee
4+
5+
# @File : 26. Remove Duplicates from Sorted Array.py
6+
# @Software: PyCharm
7+
from typing import List
8+
9+
10+
class Solution:
11+
# pop
12+
def removeDuplicates1(self, nums: List[int]) -> int:
13+
j = 0
14+
remove_list = []
15+
for i in range(1, len(nums)):
16+
if nums[i] == nums[j]:
17+
remove_list.append(i)
18+
j = i
19+
# print(remove_list)
20+
for i in remove_list[::-1]:
21+
nums.pop(i)
22+
return len(nums)
23+
24+
# swap
25+
def removeDuplicates(self, nums: List[int]) -> int:
26+
j = 1
27+
if len(nums) > 0:
28+
for i in range(1, len(nums)):
29+
if nums[i] != nums[i - 1]:
30+
nums[j] = nums[i]
31+
j += 1
32+
else:
33+
j = 0
34+
return j
35+
36+
37+
if __name__ == '__main__':
38+
A = [0, 0, 1, 1, 1, 2, 2, 3, 3, 4]
39+
print(Solution().removeDuplicates(A))
40+
print(A)

371.sum-of-two-integers.py

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# -*- coding: utf-8 -*-
2+
# @Time : 2019/2/25 11:46
3+
# @Author : xulzee
4+
5+
# @File : 371.sum-of-two-integers.py
6+
# @Software: PyCharm
7+
18
class Solution:
29
def getSum(self, a, b):
310
"""

509.fibonacci-number.py

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
# -*- coding: utf-8 -*-
2+
# @Time : 2019/2/25 11:46
3+
# @Author : xulzee
4+
5+
# @File : 509.fibonacci-number.py
6+
# @Software: PyCharm
7+
8+
19
class Solution:
210
def fib1(self, N):
311
"""

521.longest-uncommon-subsequence-i.py

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
# -*- coding: utf-8 -*-
2+
# @Time : 2019/2/25 11:46
3+
# @Author : xulzee
4+
5+
# @File : 521.longest-uncommon-subsequence-i.py
6+
# @Software: PyCharm
7+
8+
19
class Solution:
210
def findLUSlength(self, a, b):
311
"""

784.Letter Case Permutation.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# -*- coding: utf-8 -*-
2+
# @Time : 2019/2/25 11:46
3+
# @Author : xulzee
4+
5+
# @File : 784.Letter Case Permutation.py
6+
# @Software: PyCharm
7+
8+
9+
class Solution:
10+
def letterCasePermutation(self, S: 'str') -> 'List[str]':
11+
result = []
12+
if S == "":
13+
return [""]
14+
print(result)
15+
for i in S:
16+
if i.isalpha():
17+
if result == []:
18+
result.append(i.lower())
19+
result.append(i.upper())
20+
else:
21+
temp = []
22+
temp = result[:]
23+
for j in range(len(result)):
24+
result[j] += i.lower()
25+
temp[j] += i.upper()
26+
result = result + temp
27+
28+
else:
29+
if result == []:
30+
result.append(i)
31+
else:
32+
for j in range(len(result)):
33+
result[j] += i
34+
return result
35+
36+
37+
if __name__ == '__main__':
38+
S = "a1b2"
39+
print(Solution().letterCasePermutation(S))

806.repeated_n_times.py

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
# -*- coding: utf-8 -*-
2+
# @Time : 2019/2/25 11:46
3+
# @Author : xulzee
4+
5+
# @File : 806.repeated_n_times.py
6+
# @Software: PyCharm
7+
8+
19
class Solution:
210
def repeatedNTimes1(self, A):
311
"""

933.number-of-recent-calls.py

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# -*- coding: utf-8 -*-
2+
# @Time : 2019/2/25 11:46
3+
# @Author : xulzee
4+
5+
# @File : 933.number-of-recent-calls.py
6+
# @Software: PyCharm
7+
18
import collections
29

310

961.number_of_lines.py

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
# -*- coding: utf-8 -*-
2+
# @Time : 2019/2/25 11:46
3+
# @Author : xulzee
4+
5+
# @File : 961.number_of_lines.py
6+
# @Software: PyCharm
7+
8+
19
import math
210
class Solution:
311
def numberOfLines1(self, widths, S):

977.squares-of-a-sorted-array.py

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
# -*- coding: utf-8 -*-
2+
# @Time : 2019/2/25 11:46
3+
# @Author : xulzee
4+
5+
# @File : 977.squares-of-a-sorted-array.py
6+
# @Software: PyCharm
7+
8+
19
class Solution(object):
210
def sortedSquares(self, A):
311
"""

KMP.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# -*- coding: utf-8 -*-
2+
# @Time : 2019/2/25 11:46
3+
# @Author : xulzee
4+
5+
# @File : KMP.py
6+
# @Software: PyCharm
7+
8+
9+
def get_next(s: 'str'):
10+
next_list = [-1] * len(s)
11+
i, j = 0, -1
12+
while i < len(s) - 1:
13+
if j == -1 or s[i] == s[j]:
14+
i += 1
15+
j += 1
16+
if s[i] == s[j]:
17+
next_list[i] = next_list[j]
18+
else:
19+
next_list[i] = j
20+
else:
21+
j = next_list[j]
22+
print(next_list)
23+
return next_list
24+
25+
26+
def index_kmp(s: 'str', t: 'str'):
27+
i, j = 0, 0
28+
next_list = get_next(t)
29+
while i < len(s) and j < len(t):
30+
if j == -1 or s[i] == t[j]:
31+
j += 1
32+
i += 1
33+
else:
34+
j = next_list[j]
35+
if j >= len(t):
36+
return i - len(t)
37+
else:
38+
return 0
39+
40+
41+
if __name__ == '__main__':
42+
s, t = 'qwewacdabcasd','abc'
43+
print(index_kmp(s, t))
44+

0 commit comments

Comments
 (0)