Skip to content

Commit b12f412

Browse files
authored
Merge pull request #2395 from jjipper/WEEK01
[jjipper] WEEK 01 solutions
2 parents 6f7cb00 + 7f0362b commit b12f412

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

contains-duplicate/jjipper.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* https://leetcode.com/problems/contains-duplicate
3+
* time complexity : O(n)
4+
* space complexity : O(n)
5+
*/
6+
7+
export function containsDuplicate(nums: number[]): boolean {
8+
const seen = new Set<number>();
9+
for (const num of nums) {
10+
if (seen.has(num)) {
11+
return true;
12+
}
13+
seen.add(num);
14+
}
15+
return false;
16+
};

two-sum/jjipper.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)