Skip to content

Commit 6a550a2

Browse files
authored
Merge pull request algorithm004-01#244 from laxlyt/master
246-Week 01
2 parents d88f69e + 8c63152 commit 6a550a2

7 files changed

Lines changed: 247 additions & 1 deletion

File tree

Week 01/id_246/LeetCode_189_246.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'''
2+
rotate-array
3+
4+
给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数。
5+
6+
示例 1:
7+
8+
输入: [1,2,3,4,5,6,7] 和 k = 3
9+
输出: [5,6,7,1,2,3,4]
10+
解释:
11+
向右旋转 1 步: [7,1,2,3,4,5,6]
12+
向右旋转 2 步: [6,7,1,2,3,4,5]
13+
向右旋转 3 步: [5,6,7,1,2,3,4]
14+
'''
15+
16+
nums = [1,2,3,4,5,6,7]
17+
18+
#解法一
19+
def rotate_1(nums, k):
20+
k %= len(nums)
21+
nums[:] = nums[-k:]+nums[:-k]
22+
return nums
23+
24+
25+
26+
#解法二
27+
def rotate_2(nums, k):
28+
k %= len(nums)
29+
for _ in range(k):
30+
nums.insert(0, nums.pop())
31+
return nums
32+

Week 01/id_246/LeetCode_1_246.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'''
2+
two-sum_1
3+
4+
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,
5+
并返回他们的数组下标。
6+
7+
你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。
8+
9+
示例:
10+
给定 nums = [2, 7, 11, 15], target = 9
11+
12+
因为 nums[0] + nums[1] = 2 + 7 = 9
13+
所以返回 [0, 1]
14+
'''
15+
16+
nums = [2, 7, 11, 15]
17+
target = 9
18+
19+
def twoSum(nums, target):
20+
seen = {}
21+
for i, v in enumerate(nums):
22+
remaining = target - v
23+
if remaining in seen:
24+
return [seen[remaining], i]
25+
seen[v] = i

Week 01/id_246/LeetCode_21_246.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'''
2+
merge two sorted lists
3+
4+
将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
5+
6+
示例:
7+
输入:1->2->4, 1->3->4
8+
输出:1->1->2->3->4->4
9+
'''
10+
11+
#迭代
12+
l1 = create_linked_list([1,2,3,4,5])
13+
l2 = create_linked_list([6,7,8,9,10])
14+
15+
def mergeTwoLists_1(l1, l2) :
16+
# maintain an unchanging reference to node ahead of the return node.
17+
prehead = ListNode(-1)
18+
prev = prehead
19+
while l1 and l2:
20+
if l1.val <= l2.val:
21+
prev.next = l1
22+
l1 = l1.next
23+
else:
24+
prev.next = l2
25+
l2 = l2.next
26+
prev = prev.next
27+
# the non-null list to the end of the merged list.
28+
prev.next = l1 if l1 is not None else l2
29+
return prehead.next
30+
31+
32+
# 递归
33+
def mergeTwoLists_2(l1,l2):
34+
if l1 is None:
35+
return l2
36+
elif l2 is None:
37+
return l1
38+
elif l1.val < l2.val:
39+
l1.next = mergeTwoLists_2(l1.next,l2)
40+
return l1
41+
else:
42+
l2.next = mergeTwoLists_2(l1, l2.next)
43+
return l2

Week 01/id_246/LeetCode_26_246.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'''
2+
remove duplicates from sorted
3+
4+
给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度。
5+
6+
不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。
7+
8+
给定数组 nums = [1,1,2],
9+
函数应该返回新的长度 2, 并且原数组 nums 的前两个元素被修改为 1, 2。
10+
11+
你不需要考虑数组中超出新长度后面的元素。
12+
'''
13+
14+
nums = [1,1,2]
15+
16+
def removeDuplicates(nums):
17+
if nums:
18+
nums[:len(set(nums))] = sorted(set(nums))
19+
return len(set(nums))
20+
else:
21+
return 0

Week 01/id_246/LeetCode_66_246.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'''
2+
plus-one_66
3+
4+
给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一。
5+
6+
最高位数字存放在数组的首位, 数组中每个元素只存储单个数字。
7+
8+
你可以假设除了整数 0 之外,这个整数不会以零开头。
9+
10+
示例:
11+
输入: [1,2,3]
12+
输出: [1,2,4]
13+
解释: 输入数组表示数字 123。
14+
'''
15+
16+
digits = [9,0,9,9]
17+
18+
def plusOne(digits):
19+
sum_ = 0
20+
carry_ = 1
21+
22+
for i in range(len(digits)-1, -1, -1):
23+
sum_ = digits[i] + carry_
24+
carry_ = sum_ // 10
25+
digits[i] = sum_ % 10
26+
27+
if carry_ > 0:
28+
return [carry_] + digits
29+
30+
return digits

Week 01/id_246/LeetCode_88_246.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
'''
2+
merge sorted array_88
3+
4+
给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组。
5+
6+
说明:
7+
8+
初始化 nums1 和 nums2 的元素数量分别为 m 和 n。
9+
你可以假设 nums1 有足够的空间(空间大小大于或等于 m + n)来保存 nums2 中的元素。
10+
11+
示例:
12+
输入:
13+
nums1 = [1,2,3,0,0,0], m = 3
14+
nums2 = [2,5,6], n = 3
15+
16+
输出: [1,2,2,3,5,6
17+
'''
18+
19+
#合并后排序, 时间复杂度较高
20+
num1 = [1,2,3]
21+
num2 = [5,5]
22+
23+
def merge_1(num1, m, num2, n):
24+
num1[:] = sorted(num1[:m] + num2)
25+
return num1
26+
27+
28+
#双指针,从前往后
29+
30+
def merge_2(num1, m, num2, n):
31+
num1_copy = num1[:m]
32+
num1[:] = []
33+
p1 = 0
34+
p2 = 0
35+
while p1 < m and p2 < n:
36+
if num1_copy[p1] < num2[p2]:
37+
num1.append(num1_copy[p1])
38+
p1 += 1
39+
else:
40+
num1.append(num2[p2])
41+
p2 += 1
42+
43+
if p1 < m:
44+
num1[p1+p2:] = num1_copy[p1:]
45+
if p2 < n:
46+
num1[p1+p2:] = num2[p2:]
47+
return num1
48+
49+
50+
#双指针,从后往前
51+
52+
def merge_3(num1, m, num2, n):
53+
p1 = m-1
54+
p2 = n-1
55+
p = m + n -1
56+
while p1 >= 0 and p2 >= 0:
57+
if num1[p1] < num2[p2]:
58+
num1[p] = num2[p2]
59+
p2 -= 1
60+
else:
61+
num1[p] = num1[p1]
62+
p1 -= 1
63+
p -= 1
64+
num1[:p2+1] = num2[:p2+1]
65+
return num1

Week 01/id_246/NOTE.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,34 @@
11
# NOTE
22

3-
3+
第一次系统接触数据结构和算法
44

5+
第一周课程接触链表和跳表相关内容,用python实现了简单的链表结构
6+
7+
class ListNode:
8+
def __init__(self, x):
9+
self.val = x
10+
self.next = None
11+
12+
def create_linked_list(nums):
13+
if not nums:
14+
return
15+
head = prev = ListNode(nums[0])
16+
for num in nums[1:]:
17+
tmp = ListNode(num)
18+
prev.next = tmp
19+
prev = prev.next
20+
return head
21+
22+
def print_linked_list(head):
23+
nums = []
24+
while head:
25+
nums.append(head.val)
26+
head = head.next
27+
print(nums)
28+
return nums
29+
30+
head = create_linked_list([1,2,3,4,5,6,7,8,9])
31+
32+
print_linked_list(head)
33+
34+
接下来会补充其他数据结构的python实现

0 commit comments

Comments
 (0)