leetcode: add two sum
This commit is contained in:
parent
7bb5f5d546
commit
78d155ab83
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"cells": [],
|
||||
"metadata": {},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
# 1. Two Sum (Easy)
|
||||
|
||||
Given an array of integers `nums` and an integer `target`, return _indices of
|
||||
the two numbers such that they add up to `target`_.
|
||||
|
||||
You may assume that each input would have exactly **one solution**, and you may
|
||||
not use the _same_ element twice.
|
||||
|
||||
You can return the answer in any order.
|
||||
|
||||
## Example 1:
|
||||
|
||||
```
|
||||
Input: nums = [2,7,11,15], target = 9
|
||||
Output: [0,1]
|
||||
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
|
||||
```
|
||||
|
||||
## Example 2:
|
||||
|
||||
```
|
||||
Input: nums = [3,2,4], target = 6
|
||||
Output: [1,2]
|
||||
```
|
||||
|
||||
## Example 3:
|
||||
|
||||
```
|
||||
Input: nums = [3,3], target = 6
|
||||
Output: [0,1]
|
||||
```
|
||||
|
||||
## Constraints:
|
||||
|
||||
- 2 <= nums.length <= 10^4
|
||||
- -10^9 <= nums[i] <= 10^9
|
||||
- -10^9 <= target <= 10^9
|
||||
- **Only one valid answer exists.**
|
||||
|
||||
Follow-up: Can you come up with an algorithm that is less than `O(n^2)` time
|
||||
complexity?
|
|
@ -0,0 +1,115 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "92b94045-a058-4b99-bfb6-7709a2a96f1a",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# First variant O(n^2)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "764a6c5e-5fa8-41be-9ec2-2b097ed29af6",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {\n",
|
||||
" for (i, n) in nums.iter().enumerate() {\n",
|
||||
" for (k, m) in nums.iter().enumerate() {\n",
|
||||
" if i == k {\n",
|
||||
" continue;\n",
|
||||
" } else if n + m == target {\n",
|
||||
" return vec![i as i32, k as i32];\n",
|
||||
" }\n",
|
||||
" }\n",
|
||||
" }\n",
|
||||
" vec![]\n",
|
||||
"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "4278ee69-8a33-480c-a33b-1049bcec1eed",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Second variant O(n)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 24,
|
||||
"id": "7ad2762a-1deb-46ce-93fb-84b6ef637951",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"use std::collections::HashMap;\n",
|
||||
"\n",
|
||||
"pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {\n",
|
||||
" let mut hm = HashMap::<i32, i32>::new();\n",
|
||||
" \n",
|
||||
" for (i, n) in nums.into_iter().enumerate() {\n",
|
||||
" if let Some(k) = hm.get(&(target - n)) {\n",
|
||||
" return vec![*k, i as i32];\n",
|
||||
" } else {\n",
|
||||
" hm.insert(n, i as i32);\n",
|
||||
" }\n",
|
||||
" }\n",
|
||||
" vec![]\n",
|
||||
"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "d14e0757-f7eb-4c52-aab7-53a47b707377",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Tests"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 25,
|
||||
"id": "8bdf4378-b7b4-4edb-a409-4e3f98517c41",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"assert_eq!(two_sum(vec![2,7,11,15], 9), vec![0,1]);\n",
|
||||
"assert_eq!(two_sum(vec![3,2,4], 6), vec![1,2]);\n",
|
||||
"assert_eq!(two_sum(vec![3,3], 6), vec![0,1]);"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "150230c1-b43b-4275-8be7-3f35761cb19f",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "rust-Rust kernel",
|
||||
"language": "rust",
|
||||
"name": "rust-rust"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": "rust",
|
||||
"file_extension": ".rs",
|
||||
"mimetype": "text/rust",
|
||||
"name": "Rust",
|
||||
"pygment_lexer": "rust",
|
||||
"version": ""
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
Loading…
Reference in New Issue