Skip to content

Commit c4d4ce2

Browse files
committed
Add leetcode exercises
1 parent b7f7e1e commit c4d4ce2

File tree

186 files changed

+19398
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

186 files changed

+19398
-0
lines changed

Diff for: README.md

+434
Large diffs are not rendered by default.

Diff for: algorithms/001-two-sum.ipynb

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "common-configuration",
6+
"metadata": {},
7+
"source": [
8+
"给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。\n",
9+
"你可以假设每种输入只会对应一个答案。但是,数组中同一个元素不能使用两遍。\n",
10+
"\n",
11+
"#### 示例:\n",
12+
"- 给定 `nums = [2, 7, 11, 15]`, `target = 9`\n",
13+
"- 因为 `nums[0] + nums[1] = 2 + 7 = 9`\n",
14+
"- 所以返回 `[0, 1]`"
15+
]
16+
},
17+
{
18+
"cell_type": "code",
19+
"execution_count": 4,
20+
"id": "black-discovery",
21+
"metadata": {},
22+
"outputs": [],
23+
"source": [
24+
"def solve(nums, target):\n",
25+
" candidates = {}\n",
26+
" for i, n in enumerate(nums):\n",
27+
" candidates[target - n] = i\n",
28+
" for i, n in enumerate(nums):\n",
29+
" if n in candidates and i != candidates[n]:\n",
30+
" return [i, candidates[n]]\n",
31+
" return None"
32+
]
33+
},
34+
{
35+
"cell_type": "code",
36+
"execution_count": 5,
37+
"id": "refined-palestinian",
38+
"metadata": {},
39+
"outputs": [
40+
{
41+
"data": {
42+
"text/plain": [
43+
"[0, 1]"
44+
]
45+
},
46+
"execution_count": 5,
47+
"metadata": {},
48+
"output_type": "execute_result"
49+
}
50+
],
51+
"source": [
52+
"solve([2, 7, 11, 15], 9)"
53+
]
54+
},
55+
{
56+
"cell_type": "code",
57+
"execution_count": 6,
58+
"id": "early-reader",
59+
"metadata": {},
60+
"outputs": [
61+
{
62+
"name": "stdout",
63+
"output_type": "stream",
64+
"text": [
65+
"[1, 3]\n",
66+
"[1, 2]\n",
67+
"[0, 3]\n",
68+
"[2, 4]\n"
69+
]
70+
}
71+
],
72+
"source": [
73+
"print(solve([11, 7, 15, 2], 9))\n",
74+
"print(solve([3, 2, 4], 6))\n",
75+
"print(solve([0, 4, 3, 0], 0))\n",
76+
"print(solve([-1, -2, -3, -4, -5], -8))"
77+
]
78+
},
79+
{
80+
"cell_type": "code",
81+
"execution_count": null,
82+
"id": "little-demand",
83+
"metadata": {},
84+
"outputs": [],
85+
"source": []
86+
}
87+
],
88+
"metadata": {
89+
"kernelspec": {
90+
"display_name": "Python 3",
91+
"language": "python",
92+
"name": "python3"
93+
},
94+
"language_info": {
95+
"codemirror_mode": {
96+
"name": "ipython",
97+
"version": 3
98+
},
99+
"file_extension": ".py",
100+
"mimetype": "text/x-python",
101+
"name": "python",
102+
"nbconvert_exporter": "python",
103+
"pygments_lexer": "ipython3",
104+
"version": "3.8.6"
105+
}
106+
},
107+
"nbformat": 4,
108+
"nbformat_minor": 5
109+
}

Diff for: algorithms/001-two-sum.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
3+
4+
你可以假设每种输入只会对应一个答案。但是,数组中同一个元素不能使用两遍。
5+
6+
#### 示例:
7+
8+
给定 nums = [2, 7, 11, 15], target = 9
9+
因为 nums[0] + nums[1] = 2 + 7 = 9
10+
所以返回 [0, 1]
11+
"""
12+
13+
14+
def two_sum(nums, target: int):
15+
candidates = {}
16+
for i, n in enumerate(nums):
17+
# if target >= n:
18+
candidates[target - n] = i
19+
for i, n in enumerate(nums):
20+
if n in candidates and i != candidates[n]:
21+
return [i, candidates[n]]
22+
23+
24+
if __name__ == '__main__':
25+
print(two_sum([2, 7, 11, 15], 9))
26+
print(two_sum([11, 7, 15, 2], 9))
27+
print(two_sum([3, 2, 4], 6))
28+
print(two_sum([0, 4, 3, 0], 0))
29+
print(two_sum([-1, -2, -3, -4, -5], -8))

Diff for: algorithms/002-add-two-numbers.ipynb

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "stainless-cross",
6+
"metadata": {},
7+
"source": [
8+
"### [两个链表生成相加链表](https://leetcode-cn.com/problems/add-two-numbers/)\n",
9+
"\n",
10+
"给你两个**非空**的链表,表示两个非负的整数。它们每位数字都是按照**逆序**的方式存储的,并且每个节点只能存储**一位**数字。\n",
11+
"\n",
12+
"请你将两个数相加,并以相同形式返回一个表示和的链表。\n",
13+
"\n",
14+
"你可以假设除了数字`0`之外,这两个数都不会以`0`开头。\n",
15+
"\n",
16+
"#### 示例 1:\n",
17+
"```\n",
18+
"输入:l1 = [2,4,3], l2 = [5,6,4]\n",
19+
"输出:[7,0,8]\n",
20+
"解释:342 + 465 = 807.\n",
21+
"```\n",
22+
"\n",
23+
"#### 示例 2:\n",
24+
"```\n",
25+
"输入:l1 = [0], l2 = [0]\n",
26+
"输出:[0]\n",
27+
"```\n",
28+
"\n",
29+
"#### 示例 3:\n",
30+
"```\n",
31+
"输入:l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]\n",
32+
"输出:[8,9,9,9,0,0,0,1]\n",
33+
"```\n",
34+
"#### 提示:\n",
35+
"- 每个链表中的节点数在范围`[1, 100]`内\n",
36+
"- `0 <= Node.val <= 9`\n",
37+
"- 题目数据保证列表表示的数字不含前导零"
38+
]
39+
},
40+
{
41+
"cell_type": "code",
42+
"execution_count": 3,
43+
"id": "tested-consideration",
44+
"metadata": {},
45+
"outputs": [],
46+
"source": [
47+
"class ListNode:\n",
48+
" def __init__(self, val=0, next=None):\n",
49+
" self.val = val\n",
50+
" self.next = next\n",
51+
"\n",
52+
"class Solution:\n",
53+
" def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:\n",
54+
" h1, h2, ans = l1, l2, ListNode()\n",
55+
" carry, curr = 0, ans\n",
56+
" while h1 or h2:\n",
57+
" h1val = h1.val if h1 else 0\n",
58+
" h2val = h2.val if h2 else 0\n",
59+
" value = h1val + h2val + carry\n",
60+
" if value >= 10:\n",
61+
" carry = 1\n",
62+
" value = value % 10\n",
63+
" else:\n",
64+
" carry = 0\n",
65+
" curr.next = ListNode(value)\n",
66+
" if h1 is not None:\n",
67+
" h1 = h1.next\n",
68+
" if h2 is not None:\n",
69+
" h2 = h2.next\n",
70+
" curr = curr.next\n",
71+
" \n",
72+
" if carry > 0:\n",
73+
" curr.next = ListNode(carry)\n",
74+
" return ans.next"
75+
]
76+
},
77+
{
78+
"cell_type": "code",
79+
"execution_count": null,
80+
"id": "joint-retro",
81+
"metadata": {},
82+
"outputs": [],
83+
"source": []
84+
}
85+
],
86+
"metadata": {
87+
"kernelspec": {
88+
"display_name": "Python 3",
89+
"language": "python",
90+
"name": "python3"
91+
},
92+
"language_info": {
93+
"codemirror_mode": {
94+
"name": "ipython",
95+
"version": 3
96+
},
97+
"file_extension": ".py",
98+
"mimetype": "text/x-python",
99+
"name": "python",
100+
"nbconvert_exporter": "python",
101+
"pygments_lexer": "ipython3",
102+
"version": "3.8.6"
103+
}
104+
},
105+
"nbformat": 4,
106+
"nbformat_minor": 5
107+
}

Diff for: algorithms/002-add-two-numbers.py

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
"""
2+
两数相加
3+
4+
给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。
5+
6+
如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。
7+
8+
您可以假设除了数字 0 之外,这两个数都不会以 0 开头。
9+
10+
示例:
11+
12+
输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
13+
输出:7 -> 0 -> 8
14+
原因:342 + 465 = 807
15+
"""
16+
17+
18+
class ListNode:
19+
def __init__(self, x):
20+
self.val = x
21+
self.next = None
22+
23+
24+
# def add_two_numbers(l1: ListNode, l2: ListNode) -> ListNode:
25+
# p, q = l1, l2
26+
# curr = head = ListNode(None)
27+
# carry = 0
28+
# while p is not None and q is not None:
29+
# val = p.val + q.val + carry
30+
# if val >= 10:
31+
# val = val % 10
32+
# carry = 1
33+
# else:
34+
# carry = 0
35+
#
36+
# curr.next = ListNode(val)
37+
# curr = curr.next
38+
# p, q = p.next, q.next
39+
#
40+
# if carry == 0:
41+
# if p is not None:
42+
# curr.next = p
43+
# elif q is not None:
44+
# curr.next = q
45+
# else:
46+
# while p is not None:
47+
# val = p.val + carry
48+
# if val >= 10:
49+
# val = val % 10
50+
# curr.next = ListNode(val)
51+
# curr = curr.next
52+
# else:
53+
# curr.next = ListNode(val)
54+
# curr = curr.next
55+
# curr.next = p.next
56+
# carry = 0
57+
# break
58+
# p = p.next
59+
#
60+
# while q is not None:
61+
# val = q.val + carry
62+
# if val >= 10:
63+
# val = val % 10
64+
# curr.next = ListNode(val)
65+
# curr = curr.next
66+
# else:
67+
# curr.next = ListNode(val)
68+
# curr = curr.next
69+
# curr.next = q.next
70+
# carry = 0
71+
# break
72+
# q = q.next
73+
#
74+
# if carry == 1:
75+
# curr.next = ListNode(1)
76+
#
77+
# return head.next
78+
79+
80+
def add_two_numbers(l1: ListNode, l2: ListNode) -> ListNode:
81+
p, q = l1, l2
82+
curr = head = ListNode(None)
83+
carry = 0
84+
while p is not None or q is not None:
85+
pval = p.val if p is not None else 0
86+
qval = q.val if q is not None else 0
87+
val = pval + qval + carry
88+
if val >= 10:
89+
val = val % 10
90+
carry = 1
91+
else:
92+
carry = 0
93+
94+
curr.next = ListNode(val)
95+
curr = curr.next
96+
if p is not None:
97+
p = p.next
98+
if q is not None:
99+
q = q.next
100+
101+
if carry == 1:
102+
curr.next = ListNode(1)
103+
104+
return head.next
105+
106+
107+
if __name__ == '__main__':
108+
l1 = ListNode(9)
109+
l1.next = ListNode(8)
110+
l2 = ListNode(1)
111+
print(add_two_numbers(l1, l2))
112+

0 commit comments

Comments
 (0)