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 6f7cb00 commit f78c3c6Copy full SHA for f78c3c6
1 file changed
two-sum/jjipper.ts
@@ -0,0 +1,18 @@
1
+/**
2
+ * https://leetcode.com/problems/two-sum
3
+ * time complexity : O(n)
4
+ * space complexity : O(n)
5
+ */
6
+
7
+export function twoSum(nums: number[], target: number): number[] {
8
+ const obj: Record<number, number> = {};
9
+ for (let i = 0; i < nums.length; i++) {
10
+ const a = nums[i];
11
+ const b = target - a;
12
+ if (b in obj) {
13
+ return [obj[b], i];
14
+ }
15
+ obj[a] = i;
16
17
+ return [];
18
+};
0 commit comments