GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — 750-week-03 · algorithm004-05/algorithm004-05@098194d

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • Week 1/id_750
    • LeetCode_11_750.py
    • LeetCode_15_750.py
    • LeetCode_189_750.py
    • LeetCode_1_750.py
    • LeetCode_21_750-unfinished.py
    • LeetCode_26_750.py
    • LeetCode_283_750.py
    • LeetCode_66_750.py
    • LeetCode_70_750.py
    • LeetCode_88_750.py
  • Week 2/id_750
    • LeetCode_22_750.py
  • Week 5/id_750
    • LeetCode_1143_750.py
    • LeetCode_120_750.py
    • LeetCode_509_750.py
    • LeetCode_62_750.py
    • LeetCode_63_750.py
    • LeetCode_70_750.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎Week 1/id_750/LeetCode_11_750.py‎

Copy file name to clipboard
+38Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0)。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。
3+
4+
说明:你不能倾斜容器,且 n 的值至少为 2。
5+
6+
示例:
7+
8+
输入: [1,8,6,2,5,4,8,3,7]
9+
输出: 49
10+
"""
11+
12+
from typing import List
13+
14+
15+
class Solution:
16+
# 双指针思想, 从左右同时遍历,直至两个指针遇见,找到合适的结果输出
17+
def maxArea(self, height: List[int]) -> int:
18+
maxarea = 0
19+
left = 0
20+
right = len(height) - 1
21+
22+
while left < right:
23+
maxarea = max(maxarea, min(height[left], height[right]) * (right - left))
24+
if height[left] < height[right]:
25+
left += 1
26+
else:
27+
right -= 1
28+
return maxarea
29+
30+
31+
if __name__ == '__main__':
32+
solution = Solution()
33+
34+
nums = [1, 8, 6, 2, 5, 4, 8, 3, 7]
35+
36+
result = solution.maxArea(nums)
37+
38+
print(result)
Collapse file

‎Week 1/id_750/LeetCode_15_750.py‎

Copy file name to clipboard
+60Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
"""
2+
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组。
3+
4+
注意:答案中不可以包含重复的三元组。
5+
6+
例如, 给定数组 nums = [-1, 0, 1, 2, -1, -4],
7+
8+
满足要求的三元组集合为:
9+
[
10+
[-1, 0, 1],
11+
[-1, -1, 2]
12+
]
13+
14+
"""
15+
from typing import List
16+
17+
18+
class Solution:
19+
def threeSum(self, nums: List[int]) -> List[List[int]]:
20+
n = len(nums)
21+
if not nums or n < 3:
22+
return []
23+
24+
nums.sort()
25+
result_list = list()
26+
27+
for i in range(len(nums) - 2):
28+
if nums[i] > 0:
29+
return result_list
30+
if i > 0 and nums[i] == nums[i - 1]:
31+
continue
32+
L = i + 1
33+
R = n - 1
34+
while L < R:
35+
sum = nums[i] + nums[L] + nums[R]
36+
if sum < 0:
37+
L += 1
38+
while L < R and nums[L] == nums[L - 1]: L += 1
39+
elif sum > 0:
40+
R -= 1
41+
while L < R and nums[R] == nums[R + 1]: R += 1
42+
else:
43+
result_list.append([nums[i], nums[L], nums[R]])
44+
L += 1
45+
R -= 1
46+
while L < R and nums[L] == nums[L - 1]: L += 1
47+
while L < R and nums[R] == nums[R + 1]: R += 1
48+
49+
return result_list
50+
51+
52+
if __name__ == '__main__':
53+
solution = Solution()
54+
55+
nums = [-1, 0, 1, 2, -1, -4]
56+
for i, v in enumerate(nums[:-2]):
57+
print(i,v)
58+
59+
# result = solution.threeSum(nums)
60+
# print(result)
Collapse file

‎Week 1/id_750/LeetCode_189_750.py‎

Copy file name to clipboard
+47-23Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,47 @@
1-
from typing import List
2-
3-
4-
# 189.旋转数组
5-
class Solution:
6-
def rotate(self, nums: List[int], k: int) -> None:
7-
"""
8-
Do not return anything, modify nums in-place instead.
9-
"""
10-
n = len(nums)
11-
k %= n
12-
# 利用了python切片的特性
13-
nums[:] = nums[::-1]
14-
nums[:k] = nums[:k][::-1]
15-
nums[k:] = nums[k:][::-1]
16-
17-
18-
if __name__ == '__main__':
19-
s = Solution()
20-
b = [1, 2, 3, 4, 5, 6, 7, 8]
21-
k = 3
22-
23-
s.rotate(b, k)
1+
from typing import List
2+
3+
4+
# 189.旋转数组
5+
class Solution:
6+
def rotate(self, nums: List[int], k: int) -> None:
7+
"""
8+
Do not return anything, modify nums in-place instead.
9+
"""
10+
n = len(nums)
11+
k %= n
12+
# 利用了python切片的特性
13+
nums[:] = nums[::-1]
14+
nums[:k] = nums[:k][::-1]
15+
nums[k:] = nums[k:][::-1]
16+
17+
print("方法1 : %s" % nums)
18+
19+
# 从末尾开始移动到头
20+
def rotate2(self, nums: List[int], k: int) -> None:
21+
k %= len(nums)
22+
for i in range(k):
23+
nums.insert(0, nums[len(nums) - 1])
24+
nums.pop(len(nums) - 1)
25+
26+
print("方法2 : %s" % nums)
27+
28+
# 利用一个额外数组
29+
def rotate3(self, nums: List[int], k: int) -> None:
30+
tmp = list()
31+
nums_len = len(nums)
32+
for i in range(nums_len):
33+
tmp.insert((i + k) % nums_len, nums[i])
34+
for i in range(nums_len):
35+
nums[i] = tmp[i]
36+
37+
print("方法3 : %s" % nums)
38+
39+
40+
if __name__ == '__main__':
41+
s = Solution()
42+
b = [1, 2, 3, 4, 5, 6, 7, 8]
43+
k = 3
44+
# s.rotate(b, k)
45+
# s.rotate2(b, k)
46+
s.rotate3(b, k)
47+
Collapse file

‎Week 1/id_750/LeetCode_1_750.py‎

Copy file name to clipboard
+53Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"""
2+
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
3+
4+
你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。
5+
6+
示例:
7+
8+
给定 nums = [2, 7, 11, 15], target = 9
9+
10+
因为 nums[0] + nums[1] = 2 + 7 = 9
11+
所以返回 [0, 1]
12+
"""
13+
from typing import List
14+
15+
16+
class Solution:
17+
def twoSum(self, nums: List[int], target: int) -> List[int]:
18+
if len(nums) <= 1:
19+
return False
20+
dic = dict()
21+
for i in range(len(nums)):
22+
if nums[i] in dic:
23+
return [dic[nums[i]], i]
24+
else:
25+
dic[target - nums[i]] = i
26+
27+
def twoSum2(self, nums: List[int], target: int) -> List[int]:
28+
for i in range(len(nums)):
29+
for j in range(i + 1, len(nums), 1):
30+
if nums[i] + nums[j] == target:
31+
return [i, j]
32+
33+
def twoSum3(self, nums: List[int], target: int) -> List[int]:
34+
lens = len(nums)
35+
j = -1
36+
for i in range(1, lens):
37+
temp = nums[:i]
38+
if (target - nums[i]) in temp:
39+
j = temp.index(target - nums[i])
40+
break
41+
if j >= 0:
42+
return [j, i]
43+
44+
45+
if __name__ == '__main__':
46+
solution = Solution()
47+
48+
nums = [2, 5, 5, 15]
49+
target = 10
50+
51+
result = solution.twoSum3(nums, target)
52+
53+
print(result)
Collapse file

‎Week 1/id_750/LeetCode_21_750-unfinished.py‎

Copy file name to clipboard
+28Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 
3+
4+
示例:
5+
6+
输入:1->2->4, 1->3->4
7+
输出:1->1->2->3->4->4
8+
"""
9+
10+
11+
# Definition for singly-linked list.
12+
class ListNode:
13+
def __init__(self, x):
14+
self.val = x
15+
self.next = None
16+
17+
18+
class Solution:
19+
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
20+
pass
21+
22+
23+
if __name__ == '__main__':
24+
solution = Solution()
25+
l1 = [1, 2, 4]
26+
l2 = [1, 3, 4]
27+
result = solution.mergeTwoLists(l1, l2)
28+
print(result)
Collapse file

‎Week 1/id_750/LeetCode_26_750.py‎

Copy file name to clipboard
+22-19Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
1-
from typing import List
2-
3-
4-
# 26.删除排序数组中的重复项
5-
class Solution:
6-
def removeDuplicates(self, nums: List[int]) -> int:
7-
if not nums: return 0
8-
k = 1
9-
for i in range(1, len(nums)):
10-
if nums[i] != nums[i - 1]:
11-
nums[k] = nums[i]
12-
k += 1
13-
return k
14-
15-
16-
if __name__ == '__main__':
17-
s = Solution()
18-
a = [1, 1, 2]
19-
print(s.removeDuplicates(a))
1+
from typing import List
2+
3+
4+
# 26.删除排序数组中的重复项
5+
class Solution:
6+
def removeDuplicates(self, nums: List[int]) -> int:
7+
if not nums: return 0
8+
k = 1
9+
for i in range(1, len(nums)):
10+
if nums[i] != nums[i - 1]:
11+
nums[k] = nums[i]
12+
k += 1
13+
return k
14+
15+
def removeDuplicates2(self, nums: List[int]) -> int:
16+
pass
17+
18+
19+
if __name__ == '__main__':
20+
s = Solution()
21+
a = [1, 1, 2]
22+
print(s.removeDuplicates2(a))
Collapse file

‎Week 1/id_750/LeetCode_283_750.py‎

Copy file name to clipboard
+36Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""
2+
给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。
3+
4+
示例:
5+
6+
输入: [0,1,0,3,12]
7+
输出: [1,3,12,0,0]
8+
9+
说明:
10+
1.必须在原数组上操作,不能拷贝额外的数组。
11+
2.尽量减少操作次数。
12+
"""
13+
from typing import List
14+
15+
16+
class Solution:
17+
def moveZeroes(self, nums: List[int]) -> None:
18+
i = 0
19+
for j in range(len(nums)):
20+
if nums[j] != 0:
21+
nums[i] = nums[j]
22+
i += 1
23+
24+
for k in range(i, len(nums), 1):
25+
nums[k] = 0
26+
print(nums)
27+
28+
29+
if __name__ == '__main__':
30+
solution = Solution()
31+
32+
nums = [0, 1, 0, 3, 12]
33+
34+
result = solution.moveZeroes(nums)
35+
36+
# print(result)
Collapse file

‎Week 1/id_750/LeetCode_66_750.py‎

Copy file name to clipboard
+37Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""
2+
给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一。
3+
4+
最高位数字存放在数组的首位, 数组中每个元素只存储单个数字。
5+
6+
你可以假设除了整数 0 之外,这个整数不会以零开头。
7+
8+
示例 1:
9+
10+
输入: [1,2,3]
11+
输出: [1,2,4]
12+
解释: 输入数组表示数字 123。
13+
示例 2:
14+
15+
输入: [4,3,2,1]
16+
输出: [4,3,2,2]
17+
解释: 输入数组表示数字 4321。
18+
"""
19+
from typing import List
20+
21+
22+
class Solution:
23+
def plusOne(self, digits: List[int]) -> List[int]:
24+
for i in range(len(digits)):
25+
digits[i] += 1
26+
digits[i] %= 10
27+
if digits[i] != 0:
28+
return digits
29+
digits.insert(0, 1)
30+
return digits
31+
32+
33+
if __name__ == '__main__':
34+
solution = Solution()
35+
digits = [2, 9, 9]
36+
result = solution.plusOne(digits)
37+
print(result)

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.