Skip to content

Commit f966843

Browse files
authored
Merge pull request #2119 from 1lsang/main
[1lsang] WEEK 03 solutions
2 parents f6a9a4e + 2ed50ad commit f966843

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

combination-sum/1lsang.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function combinationSum(candidates: number[], target: number): number[][] {
2+
// arr: index까지 갈 수 있는 combinationSum
3+
const arr:number[][][] = Array.from({ length: target + 1 }, () => [] as number[][]);
4+
// 0을 만들 수 있는 방법은 숫자가 없는 것
5+
arr[0].push([] as number[]);
6+
7+
for (const candidate of candidates) {
8+
for (let n = candidate; n <= target; n++) {
9+
for (const combination of arr[n-candidate]) {
10+
arr[n].push([...combination, candidate]);
11+
}
12+
}
13+
}
14+
console.log(arr);
15+
return arr[target];
16+
};

number-of-1-bits/1lsang.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function hammingWeight(n: number): number {
2+
// 최대 이진수 찾기
3+
let s = 1;
4+
while (s*2 <= n) {
5+
s*=2;
6+
}
7+
8+
// bit 세기
9+
let cnt = 0;
10+
while (n > 0) {
11+
if (n - s >= 0) {
12+
n -= s;
13+
cnt++;
14+
}
15+
s /= 2;
16+
}
17+
return cnt;
18+
};

valid-palindrome/1lsang.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function isPalindrome(s: string): boolean {
2+
// console.log('A'.charCodeAt(0), 'Z'.charCodeAt(0)); // 65 90
3+
// console.log('a'.charCodeAt(0), 'z'.charCodeAt(0)); // 97 122
4+
// console.log('0'.charCodeAt(0), '9'.charCodeAt(0)); // 48 57
5+
6+
// 문자열 변환 과정
7+
let converted = ''
8+
for (let c of s) {
9+
const charCode = c.charCodeAt(0);
10+
if (charCode >= 65 && charCode <= 90) {
11+
converted += c.toLowerCase();
12+
}
13+
else if ((charCode >= 97 && charCode <= 122) || (charCode >= 48 && charCode <= 57)) {
14+
converted += c;
15+
}
16+
}
17+
18+
// palindrome 판단 조건
19+
const length = converted.length;
20+
21+
for (let i = 0; i < length/2; i ++) {
22+
if (converted[i] !== converted[length - 1 - i]) return false;
23+
}
24+
25+
return true;
26+
};

0 commit comments

Comments
 (0)