Skip to content

Commit 2ad68c3

Browse files
committed
week01 assignment
1 parent 0fd6bbe commit 2ad68c3

9 files changed

Lines changed: 136 additions & 1 deletion

.idea/.gitignore

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Week01/NOTE.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,16 @@
1-
学习笔记
1+
#学习笔记
2+
+ 如果需要对一个数组变量,进行原地操作,要在数组变量名字的后面加上[:]
3+
+ 如何实现链表\[python\]
4+
+ 链表的类
5+
+ 哑节点的使用可以避免代码进行冗余的判断
6+
```python
7+
class ListNode:
8+
def __init__(self, val=0, next=None):
9+
self.val = val
10+
self.next = next
11+
12+
dummy = ListNode(0) #作为头节点使用,具体的值不重要,最后输出dummy.next即可
13+
```
14+
15+
16+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from typing import List
2+
class Solution:
3+
def removeDuplicates(self, nums: List[int]) -> int:
4+
curr = 0
5+
for i in range(1,len(nums)):
6+
if nums[i] != nums[curr]:
7+
curr += 1
8+
nums[curr] = nums[i]
9+
return curr+1
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from typing import List
2+
class Solution:
3+
def rotate(self, nums: List[int], k: int) -> None:
4+
"""
5+
Do not return anything, modify nums in-place instead.
6+
"""
7+
nums[:] = nums[len(nums)-k%len(nums):]+nums[:len(nums)-k%len(nums)]
8+
return
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from typing import List
2+
class Solution:
3+
def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:
4+
"""
5+
Do not return anything, modify nums1 in-place instead.
6+
"""
7+
p = m-1
8+
q = n-1
9+
while p >= 0 and q >= 0:
10+
if nums1[p] < nums2[q]:
11+
nums1[p+q+1] = nums2[q]
12+
q -= 1
13+
else:
14+
nums1[p+q+1] = nums1[p]
15+
p -= 1
16+
nums1[:q+1] = nums2[:q+1]
17+
return nums1
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class ListNode:
2+
def __init__(self, val=0, next=None):
3+
self.val = val
4+
self.next = next
5+
6+
7+
class Solution:
8+
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
9+
if not l1 or not l2:
10+
return l1 or l2
11+
if l1.val <= l2.val:
12+
l1.next = self.mergeTwoLists(l1.next, l2)
13+
return l1
14+
else:
15+
l2.next = self.mergeTwoLists(l1, l2.next)
16+
return l2

Week01/assignment05_L1_two sum.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from typing import List
2+
class Solution:
3+
def twoSum(self, nums: List[int], target: int) -> List[int]:
4+
d = {}
5+
for i,n in enumerate (nums):
6+
m = target - n
7+
if m in d:
8+
return [d[m],i]
9+
else:
10+
d[n] = i
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from typing import List
2+
3+
class Solution:
4+
def moveZeroes(self, nums: List[int]) -> None:
5+
"""
6+
Do not return anything, modify nums in-place instead.
7+
"""
8+
9+
# 统计0的个数,添在数组后面
10+
# unzero_counter = 0
11+
# for j in range(len(nums)):
12+
# if nums[j] != 0:
13+
# nums[unzero_counter] = nums[j]
14+
# unzero_counter += 1
15+
# zero_counter = len(nums)-unzero_counter
16+
# nums[unzero_counter:] = [0] * (zero_counter)
17+
18+
# 双下标
19+
# unzero_counter = 0
20+
# for i in range(len(nums)):
21+
# if nums[i] != 0:
22+
# nums[unzero_counter] = nums[i]
23+
# if i != unzero_counter:
24+
# nums[i] = 0
25+
# unzero_counter += 1
26+
27+
# 交换位置
28+
j = 0
29+
for i in range(len(nums)):
30+
if nums[i] != 0:
31+
nums[j], nums[i] = nums[i], nums[j]
32+
j += 1
33+
34+
return nums
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from typing import List
2+
class Solution:
3+
def plusOne(self, digits: List[int]) -> List[int]:
4+
5+
# # string 解法
6+
# str_digits = [str(i) for i in digits]
7+
# new_digits = int("".join(str_digits))
8+
# new_digits += 1
9+
# res = [int(j) for j in str(new_digits)]
10+
# return res
11+
12+
# recursion
13+
if digits == []:
14+
return [1]
15+
elif digits[-1] < 9:
16+
return digits[:-1]+[digits[-1]+1]
17+
else:
18+
return self.plusOne(digits[:-1])+[0]

0 commit comments

Comments
 (0)