Skip to content

Commit 4c8b68f

Browse files
authored
Merge pull request #760 from mike2ox/main
[moonhyeok] Week 2
2 parents b98effe + 5823714 commit 4c8b68f

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

3sum/mike2ox.ts

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
function threeSum(nums: number[]): number[][] {
2+
if (nums.length < 3) return [];
3+
const result: number[][] = [];
4+
const checked = new Set<string>();
5+
const numMap = new Map<number, number>();
6+
7+
// 중복 결과 방지
8+
nums.sort((a, b) => a - b);
9+
// Map에 모든 값과 인덱스 저장
10+
nums.forEach((num, index) => numMap.set(num, index));
11+
12+
for (let i = 0; i < nums.length - 2; i++) {
13+
if (nums[i] > 0) break; // 양수면 존재 X
14+
// 중복된 첫 번째 수 건너뛰기
15+
if (i > 0 && nums[i] === nums[i - 1]) continue;
16+
17+
for (let j = i + 1; j < nums.length - 1; j++) {
18+
// 중복된 두 번째 수 건너뛰기
19+
if (j > i + 1 && nums[j] === nums[j - 1]) continue;
20+
// 세 번째 수 계산
21+
const target = -(nums[i] + nums[j]);
22+
23+
// Map을 사용하여 세 번째 수 검색
24+
if (numMap.has(target)) {
25+
const k = numMap.get(target)!;
26+
if (k > j) {
27+
// k가 j보다 커야 중복 방지
28+
const triplet = [nums[i], nums[j], nums[k]];
29+
const key = triplet.join(",");
30+
31+
// Set을 사용하여 중복 결과 방지
32+
if (!checked.has(key)) {
33+
checked.add(key);
34+
result.push(triplet);
35+
}
36+
}
37+
}
38+
}
39+
}
40+
41+
return result;
42+
}

climbing-stairs/mike2ox.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function climbStairs(n: number): number {
2+
let result = 0;
3+
let step1 = 1;
4+
let step2 = 0;
5+
6+
for (let i = 0; i < n; i++) {
7+
result = step1 + step2;
8+
step2 = step1;
9+
step1 = result;
10+
}
11+
return result;
12+
}

valid-anagram/mike2ox.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
function isAnagram(s: string, t: string): boolean {
2+
if (s.length !== t.length) return false;
3+
return s.split("").sort().join() === t.split("").sort().join();
4+
}

0 commit comments

Comments
 (0)