diff options
Diffstat (limited to 'two-sum')
-rw-r--r-- | two-sum/README.md | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/two-sum/README.md b/two-sum/README.md new file mode 100644 index 0000000..28150e9 --- /dev/null +++ b/two-sum/README.md @@ -0,0 +1,26 @@ +# Two Sum + +## Использование хэщ-таблицы + +В этом решении используется хэш-таблица. + +- Сложность: `O(n)`. +- Плюс: Скорость. +- Минус: Память. + +```rust +pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> { + let mut hash: std::collections::HashMap<i32, usize> = std::collections::HashMap::new(); + + for (i, x) in nums.iter().enumerate() { + if hash.contains_key(x) { + return vec![*hash.get(x).unwrap() as i32, i as i32]; + } + + let res = target - x; + hash.insert(res, i); + } + + panic!("No solution found") +} +```
\ No newline at end of file |