Skip to content

Commit 1aa1a45

Browse files
committed
2 parents 9797cb1 + bcaaeef commit 1aa1a45

File tree

8 files changed

+513
-0
lines changed

8 files changed

+513
-0
lines changed

.vscode/launch.json

+3
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@
1010
"request": "launch",
1111
"program": "${file}",
1212
"console": "none",
13+
<<<<<<< HEAD
1314
"args": [
1415
"${file}"
1516
],
1617

1718

1819

20+
=======
21+
>>>>>>> bcaaeef0772a5a5e51387d27dd6c3407a2be148b
1922
},
2023
{
2124
"name": "Python: Attach",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/usr/bin/python3
2+
# -*- coding: utf-8 -*-
3+
'''
4+
AC
5+
'''
6+
7+
import time
8+
9+
10+
class Solution:
11+
def searchMatrix(self, matrix, target):
12+
"""
13+
:type matrix: List[List[int]]
14+
:type target: int
15+
:rtype: bool
16+
"""
17+
if not matrix or not matrix[0]:
18+
return False
19+
len_m = len(matrix)
20+
for i in range(len_m):
21+
if matrix[i][0]>target:
22+
break
23+
elif matrix[i][-1]>=target and matrix[i][0] <= target:
24+
try:
25+
matrix[i].index(target)
26+
return True
27+
except ValueError:
28+
pass
29+
return False
30+
31+
32+
if __name__ == "__main__":
33+
34+
t0 = time.perf_counter()
35+
36+
test_list = [[
37+
[1, 4, 7, 11, 15],
38+
[2, 5, 8, 12, 19],
39+
[3, 6, 9, 16, 22],
40+
[10, 13, 14, 17, 24],
41+
[18, 21, 23, 26, 30],
42+
], [[4, 5, 6, 7, 8, 9, 21]], [[]]]
43+
test_list_2 = [17, 6, 1]
44+
answer_list = [True, True, False]
45+
46+
test = Solution()
47+
for i in range(len(test_list)):
48+
out_t = test.searchMatrix(test_list[i], test_list_2[i])
49+
50+
if out_t == answer_list[i]:
51+
print("\033[1;32;40m Pass \033[0m")
52+
else:
53+
print(
54+
"\033[1;31;40m Fail!!\033[0m\033[0;34;40m out \"%s\" should \"%s\" by \"%.50s\" "
55+
% (out_t, answer_list[i], test_list[i]))
56+
57+
print("\nRun Time is %f s" % (time.perf_counter() - t0))
58+
59+
'''
60+
class Solution:
61+
def searchMatrix(self, matrix, target):
62+
"""
63+
:type matrix: List[List[int]]
64+
:type target: int
65+
:rtype: bool
66+
"""
67+
if len(matrix)==0 or len(matrix[0])==0:
68+
return False
69+
for i in range(len(matrix)):
70+
if target==matrix[i][-1] or target==matrix[i][0]:
71+
return True
72+
elif target<matrix[i][-1] and target>matrix[i][0]:
73+
left,right=0,len(matrix[i])-1
74+
while left<=right:
75+
mid=(left+right)//2
76+
if target==matrix[i][mid]:
77+
return True
78+
elif target>matrix[i][mid]:
79+
left=mid+1
80+
else:
81+
right=mid-1
82+
return False
83+
'''

leecode/不同路径/uniquePaths.py

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#!/usr/bin/python3
2+
# -*- coding: utf-8 -*-
3+
'''
4+
AC
5+
'''
6+
7+
import time
8+
9+
10+
class Solution:
11+
def uniquePaths(self, m, n):
12+
"""
13+
:type m: int
14+
:type n: int
15+
:rtype: int
16+
"""
17+
if m < 1 or n < 1:
18+
return 0
19+
record = [[1]*n for j in range(m)]
20+
for i in range(1, m):
21+
for j in range(1, n):
22+
record[i][j] = record[i - 1][j] + record[i][j - 1]
23+
return record[m - 1][n - 1]
24+
25+
26+
if __name__ == "__main__":
27+
28+
t0 = time.perf_counter()
29+
30+
test_list = [3, 7, 23]
31+
test_list_2 = [2, 3, 12]
32+
answer_list = [3, 28, 193536720]
33+
34+
test = Solution()
35+
for i in range(len(test_list)):
36+
out_t = test.uniquePaths(test_list[i], test_list_2[i])
37+
38+
if out_t == answer_list[i]:
39+
print("\033[1;32;40m Pass \033[0m")
40+
else:
41+
print(
42+
"\033[1;31;40m Fail!!\033[0m\033[0;34;40m out \"%s\" should \"%s\" by \"%.50s\" "
43+
% (out_t, answer_list[i],
44+
str(test_list[i]) + " " + str(test_list_2[i])))
45+
46+
print("\nRun Time is %f s" % (time.perf_counter() - t0))
47+
48+
'''
49+
50+
class Solution:
51+
def uniquePaths(self, m, n):
52+
"""
53+
:type m: int
54+
:type n: int
55+
:rtype: int
56+
超时C(m + n - 2, n - 1):
57+
if m < n :
58+
m,n = n,m
59+
combins = [c for c in itertools.combinations(range(m+n-2), n-1)]
60+
out = len(combins)
61+
return out
62+
63+
from functools import reduce
64+
if m < n:
65+
m, n = n, m
66+
mul = lambda x, y: reduce(operator.mul, range(x, y), 1)
67+
return int(mul(m, m + n - 1) / mul(1, n))
68+
"""
69+
v = [[1] * n for x in range(m) ]
70+
for i in range(m):
71+
for j in range(n):
72+
if i >= 1 and j >= 1:
73+
v[i][j] = v[i][j-1]+v[i-1][j]
74+
return v[m-1][n-1]
75+
76+
77+
class Solution:
78+
def uniquePaths(self, m, n):
79+
"""
80+
:type m: int
81+
:type n: int
82+
:rtype: int
83+
"""
84+
dp = [0 for col in range(n)]
85+
for i in range(n):
86+
dp[i] = 1
87+
88+
for i in range(1,m):
89+
for j in range(1,n):
90+
dp[j] = dp[j-1] + dp[j]
91+
92+
return dp[n-1]
93+
'''

leecode/分类颜色/sortColors.py

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/usr/bin/python3
2+
# -*- coding: utf-8 -*-
3+
'''
4+
AC
5+
'''
6+
7+
import time
8+
9+
10+
class Solution:
11+
def sortColors(self, nums):
12+
"""
13+
:type nums: List[int]
14+
:rtype: void Do not return anything, modify nums in-place instead.
15+
"""
16+
if nums == []:
17+
return nums
18+
len_t = len(nums)
19+
front_pos = 0
20+
21+
# while front_pos < len_t and nums[front_pos] == 0:
22+
# front_pos += 1
23+
24+
back_pos = len_t - 1
25+
# while back_pos > 0 and nums[back_pos] == 2:
26+
# back_pos -= 1
27+
28+
i = front_pos
29+
while i <= back_pos:
30+
temp = nums[i]
31+
if temp == 0:
32+
nums[front_pos], nums[i] = nums[i], nums[front_pos]
33+
front_pos += 1
34+
i += 1
35+
elif temp == 2:
36+
nums[back_pos], nums[i] = nums[i], nums[back_pos]
37+
back_pos -= 1
38+
# while back_pos > 0 and nums[back_pos] == 2:
39+
# back_pos -= 1
40+
else:
41+
i+=1
42+
43+
44+
if __name__ == "__main__":
45+
46+
t0 = time.perf_counter()
47+
48+
test_list = [[2, 0, 2, 1, 1, 0], [0,2,2,2,0,1,1],[]]
49+
answer_list = [[0, 0, 1, 1, 2, 2], [0,0,1,1,2,2,2],[]]
50+
test = Solution()
51+
for i in range(len(test_list)):
52+
test.sortColors(test_list[i])
53+
out_t = test_list[i]
54+
if out_t == answer_list[i]:
55+
print("\033[1;32;40m Pass \033[0m")
56+
else:
57+
print(
58+
"\033[1;31;40m Fail!!\033[0m\033[0;34;40m out \"%s\" should \"%s\" by \"%.50s\" "
59+
% (out_t, answer_list[i], test_list[i]))
60+
61+
print("\nRun Time is %f s" % (time.perf_counter() - t0))
62+
63+
64+
'''
65+
I think my solution is best,hhh
66+
'''

leecode/合并区间/merge.py

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#!/usr/bin/python3
2+
# -*- coding: utf-8 -*-
3+
'''
4+
AC
5+
'''
6+
7+
import time
8+
9+
10+
# Definition for an interval.
11+
class Interval:
12+
def __init__(self, s=0, e=0):
13+
self.start = s
14+
self.end = e
15+
16+
17+
class Solution:
18+
def merge(self, intervals):
19+
"""
20+
:type intervals: List[Interval]
21+
:rtype: List[Interval]
22+
"""
23+
out_t = []
24+
intervals.sort(key=lambda x: x.start)
25+
for val in intervals:
26+
if not out_t or val.start > out_t[-1].end:
27+
out_t.append(val)
28+
else:
29+
out_t[-1].end = max(val.end,out_t[-1].end)
30+
return out_t
31+
32+
33+
if __name__ == "__main__":
34+
35+
t0 = time.perf_counter()
36+
37+
test_list = [[[1, 3], [15, 18], [2, 6], [8, 10]], [[1, 4], [10, 15],
38+
[2, 12]], []]
39+
answer_list = [[[1, 6], [8, 10], [15, 18]], [[1, 15]], []]
40+
test = Solution()
41+
for i in range(len(test_list)):
42+
test_par = []
43+
for list_t in test_list[i]:
44+
test_par.append(Interval(list_t[0], list_t[1]))
45+
out_t = test.merge(test_par)
46+
out_tt = []
47+
for out_interval in out_t:
48+
out_tt.append([out_interval.start, out_interval.end])
49+
if out_tt == answer_list[i]:
50+
print("\033[1;32;40m Pass \033[0m")
51+
else:
52+
print(
53+
"\033[1;31;40m Fail!!\033[0m\033[0;34;40m out \"%s\" should \"%s\" by \"%.50s\" "
54+
% (out_tt, answer_list[i], test_list[i]))
55+
56+
print("\nRun Time is %f s" % (time.perf_counter() - t0))
57+
'''
58+
def merge(self, intervals):
59+
"""
60+
:type intervals: List[Interval]
61+
:rtype: List[Interval]
62+
"""
63+
n = len(intervals)
64+
starts = []
65+
ends = []
66+
for i in range(n):
67+
starts.append(intervals[i].start)
68+
ends.append(intervals[i].end)
69+
70+
starts.sort();
71+
ends.sort();
72+
res = []
73+
j=0
74+
for i in range(n):
75+
if i == n - 1 or starts[i + 1] > ends[i]:
76+
res.append([starts[j], ends[i]])
77+
j = i + 1;
78+
return res;
79+
80+
class Solution:
81+
def merge(self, intervals):
82+
intervals.sort(key=lambda x: x.start)
83+
84+
merged = []
85+
for interval in intervals:
86+
# if the list of merged intervals is empty or if the current
87+
# interval does not overlap with the previous, simply append it.
88+
if not merged or merged[-1].end < interval.start:
89+
merged.append(interval)
90+
else:
91+
# otherwise, there is overlap, so we merge the current and previous
92+
# intervals.
93+
merged[-1].end = max(merged[-1].end, interval.end)
94+
95+
return merged
96+
'''

0 commit comments

Comments
 (0)