aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2023-09-05 20:25:29 +0300
committerValentin Popov <valentin@popov.link>2023-09-05 20:25:29 +0300
commit2f67f65aeb40760d042c4feb882e8f907588cc1c (patch)
treec414ac09c25f10d5df1e72dce80fede83c9ba443
parentbc7da4bc15ef7a2eb5e301c21a6891bf3cf758cf (diff)
downloadleetcode-2f67f65aeb40760d042c4feb882e8f907588cc1c.tar.xz
leetcode-2f67f65aeb40760d042c4feb882e8f907588cc1c.zip
Решение с хэш-таблицей
-rw-r--r--two-sum/README.md26
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