-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
186 changed files
with
19,398 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"id": "common-configuration", | ||
"metadata": {}, | ||
"source": [ | ||
"给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。\n", | ||
"你可以假设每种输入只会对应一个答案。但是,数组中同一个元素不能使用两遍。\n", | ||
"\n", | ||
"#### 示例:\n", | ||
"- 给定 `nums = [2, 7, 11, 15]`, `target = 9`\n", | ||
"- 因为 `nums[0] + nums[1] = 2 + 7 = 9`\n", | ||
"- 所以返回 `[0, 1]`" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"id": "black-discovery", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"def solve(nums, target):\n", | ||
" candidates = {}\n", | ||
" for i, n in enumerate(nums):\n", | ||
" candidates[target - n] = i\n", | ||
" for i, n in enumerate(nums):\n", | ||
" if n in candidates and i != candidates[n]:\n", | ||
" return [i, candidates[n]]\n", | ||
" return None" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"id": "refined-palestinian", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"[0, 1]" | ||
] | ||
}, | ||
"execution_count": 5, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"solve([2, 7, 11, 15], 9)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 6, | ||
"id": "early-reader", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"[1, 3]\n", | ||
"[1, 2]\n", | ||
"[0, 3]\n", | ||
"[2, 4]\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"print(solve([11, 7, 15, 2], 9))\n", | ||
"print(solve([3, 2, 4], 6))\n", | ||
"print(solve([0, 4, 3, 0], 0))\n", | ||
"print(solve([-1, -2, -3, -4, -5], -8))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "little-demand", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.8.6" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
""" | ||
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。 | ||
你可以假设每种输入只会对应一个答案。但是,数组中同一个元素不能使用两遍。 | ||
#### 示例: | ||
给定 nums = [2, 7, 11, 15], target = 9 | ||
因为 nums[0] + nums[1] = 2 + 7 = 9 | ||
所以返回 [0, 1] | ||
""" | ||
|
||
|
||
def two_sum(nums, target: int): | ||
candidates = {} | ||
for i, n in enumerate(nums): | ||
# if target >= n: | ||
candidates[target - n] = i | ||
for i, n in enumerate(nums): | ||
if n in candidates and i != candidates[n]: | ||
return [i, candidates[n]] | ||
|
||
|
||
if __name__ == '__main__': | ||
print(two_sum([2, 7, 11, 15], 9)) | ||
print(two_sum([11, 7, 15, 2], 9)) | ||
print(two_sum([3, 2, 4], 6)) | ||
print(two_sum([0, 4, 3, 0], 0)) | ||
print(two_sum([-1, -2, -3, -4, -5], -8)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"id": "stainless-cross", | ||
"metadata": {}, | ||
"source": [ | ||
"### [两个链表生成相加链表](https://leetcode-cn.com/problems/add-two-numbers/)\n", | ||
"\n", | ||
"给你两个**非空**的链表,表示两个非负的整数。它们每位数字都是按照**逆序**的方式存储的,并且每个节点只能存储**一位**数字。\n", | ||
"\n", | ||
"请你将两个数相加,并以相同形式返回一个表示和的链表。\n", | ||
"\n", | ||
"你可以假设除了数字`0`之外,这两个数都不会以`0`开头。\n", | ||
"\n", | ||
"#### 示例 1:\n", | ||
"```\n", | ||
"输入:l1 = [2,4,3], l2 = [5,6,4]\n", | ||
"输出:[7,0,8]\n", | ||
"解释:342 + 465 = 807.\n", | ||
"```\n", | ||
"\n", | ||
"#### 示例 2:\n", | ||
"```\n", | ||
"输入:l1 = [0], l2 = [0]\n", | ||
"输出:[0]\n", | ||
"```\n", | ||
"\n", | ||
"#### 示例 3:\n", | ||
"```\n", | ||
"输入:l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]\n", | ||
"输出:[8,9,9,9,0,0,0,1]\n", | ||
"```\n", | ||
"#### 提示:\n", | ||
"- 每个链表中的节点数在范围`[1, 100]`内\n", | ||
"- `0 <= Node.val <= 9`\n", | ||
"- 题目数据保证列表表示的数字不含前导零" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"id": "tested-consideration", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"class ListNode:\n", | ||
" def __init__(self, val=0, next=None):\n", | ||
" self.val = val\n", | ||
" self.next = next\n", | ||
"\n", | ||
"class Solution:\n", | ||
" def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:\n", | ||
" h1, h2, ans = l1, l2, ListNode()\n", | ||
" carry, curr = 0, ans\n", | ||
" while h1 or h2:\n", | ||
" h1val = h1.val if h1 else 0\n", | ||
" h2val = h2.val if h2 else 0\n", | ||
" value = h1val + h2val + carry\n", | ||
" if value >= 10:\n", | ||
" carry = 1\n", | ||
" value = value % 10\n", | ||
" else:\n", | ||
" carry = 0\n", | ||
" curr.next = ListNode(value)\n", | ||
" if h1 is not None:\n", | ||
" h1 = h1.next\n", | ||
" if h2 is not None:\n", | ||
" h2 = h2.next\n", | ||
" curr = curr.next\n", | ||
" \n", | ||
" if carry > 0:\n", | ||
" curr.next = ListNode(carry)\n", | ||
" return ans.next" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "joint-retro", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.8.6" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
""" | ||
两数相加 | ||
给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。 | ||
如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。 | ||
您可以假设除了数字 0 之外,这两个数都不会以 0 开头。 | ||
示例: | ||
输入:(2 -> 4 -> 3) + (5 -> 6 -> 4) | ||
输出:7 -> 0 -> 8 | ||
原因:342 + 465 = 807 | ||
""" | ||
|
||
|
||
class ListNode: | ||
def __init__(self, x): | ||
self.val = x | ||
self.next = None | ||
|
||
|
||
# def add_two_numbers(l1: ListNode, l2: ListNode) -> ListNode: | ||
# p, q = l1, l2 | ||
# curr = head = ListNode(None) | ||
# carry = 0 | ||
# while p is not None and q is not None: | ||
# val = p.val + q.val + carry | ||
# if val >= 10: | ||
# val = val % 10 | ||
# carry = 1 | ||
# else: | ||
# carry = 0 | ||
# | ||
# curr.next = ListNode(val) | ||
# curr = curr.next | ||
# p, q = p.next, q.next | ||
# | ||
# if carry == 0: | ||
# if p is not None: | ||
# curr.next = p | ||
# elif q is not None: | ||
# curr.next = q | ||
# else: | ||
# while p is not None: | ||
# val = p.val + carry | ||
# if val >= 10: | ||
# val = val % 10 | ||
# curr.next = ListNode(val) | ||
# curr = curr.next | ||
# else: | ||
# curr.next = ListNode(val) | ||
# curr = curr.next | ||
# curr.next = p.next | ||
# carry = 0 | ||
# break | ||
# p = p.next | ||
# | ||
# while q is not None: | ||
# val = q.val + carry | ||
# if val >= 10: | ||
# val = val % 10 | ||
# curr.next = ListNode(val) | ||
# curr = curr.next | ||
# else: | ||
# curr.next = ListNode(val) | ||
# curr = curr.next | ||
# curr.next = q.next | ||
# carry = 0 | ||
# break | ||
# q = q.next | ||
# | ||
# if carry == 1: | ||
# curr.next = ListNode(1) | ||
# | ||
# return head.next | ||
|
||
|
||
def add_two_numbers(l1: ListNode, l2: ListNode) -> ListNode: | ||
p, q = l1, l2 | ||
curr = head = ListNode(None) | ||
carry = 0 | ||
while p is not None or q is not None: | ||
pval = p.val if p is not None else 0 | ||
qval = q.val if q is not None else 0 | ||
val = pval + qval + carry | ||
if val >= 10: | ||
val = val % 10 | ||
carry = 1 | ||
else: | ||
carry = 0 | ||
|
||
curr.next = ListNode(val) | ||
curr = curr.next | ||
if p is not None: | ||
p = p.next | ||
if q is not None: | ||
q = q.next | ||
|
||
if carry == 1: | ||
curr.next = ListNode(1) | ||
|
||
return head.next | ||
|
||
|
||
if __name__ == '__main__': | ||
l1 = ListNode(9) | ||
l1.next = ListNode(8) | ||
l2 = ListNode(1) | ||
print(add_two_numbers(l1, l2)) | ||
|
Oops, something went wrong.