We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent f291ae8 commit 65c57e6Copy full SHA for 65c57e6
two-sum/soobing.ts
@@ -0,0 +1,27 @@
1
+// 1. Brute force
2
+function twoSum(nums: number[], target: number): number[] {
3
+ for (let i = 0; i < nums.length - 1; i++) {
4
+ for (let j = i + 1; j < nums.length; j++) {
5
+ if (nums[i] + nums[j] === target) {
6
+ return [i, j];
7
+ }
8
9
10
+ return [];
11
+}
12
+
13
+// 2. Hashmap
14
15
+ const map = new Map();
16
+ for (let i = 0; i < nums.length; i++) {
17
+ map.set(nums[i], i);
18
19
20
21
+ const targetIndex = map.get(target - nums[i]);
22
+ if (targetIndex && targetIndex !== i) {
23
+ return [i, targetIndex];
24
25
26
27
0 commit comments