Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions combination-sum/1lsang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function combinationSum(candidates: number[], target: number): number[][] {
// arr: index까지 갈 수 있는 combinationSum
const arr:number[][][] = Array.from({ length: target + 1 }, () => [] as number[][]);
// 0을 만들 수 있는 방법은 숫자가 없는 것
arr[0].push([] as number[]);

for (const candidate of candidates) {
for (let n = candidate; n <= target; n++) {
for (const combination of arr[n-candidate]) {
arr[n].push([...combination, candidate]);
}
}
}
console.log(arr);
return arr[target];
};
18 changes: 18 additions & 0 deletions number-of-1-bits/1lsang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function hammingWeight(n: number): number {
// 최대 이진수 찾기
let s = 1;
while (s*2 <= n) {
s*=2;
}

// bit 세기
let cnt = 0;
while (n > 0) {
if (n - s >= 0) {
n -= s;
cnt++;
}
s /= 2;
}
return cnt;
};
26 changes: 26 additions & 0 deletions valid-palindrome/1lsang.ts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

풀이 잘 보았습니다!
문자열 변환을 regex로 하는 방법도 있어서 코드 첨부드려요!
s = s.toLowerCase().replace(/[^a-z0-9]/g, '');

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
function isPalindrome(s: string): boolean {
// console.log('A'.charCodeAt(0), 'Z'.charCodeAt(0)); // 65 90
// console.log('a'.charCodeAt(0), 'z'.charCodeAt(0)); // 97 122
// console.log('0'.charCodeAt(0), '9'.charCodeAt(0)); // 48 57

// 문자열 변환 과정
let converted = ''
for (let c of s) {
const charCode = c.charCodeAt(0);
if (charCode >= 65 && charCode <= 90) {
converted += c.toLowerCase();
}
else if ((charCode >= 97 && charCode <= 122) || (charCode >= 48 && charCode <= 57)) {
converted += c;
}
}

// palindrome 판단 조건
const length = converted.length;

for (let i = 0; i < length/2; i ++) {
if (converted[i] !== converted[length - 1 - i]) return false;
}

return true;
};