Skip to content

Commit 269c967

Browse files
authored
Merge pull request #2113 from Blossssom/main
[Blossssom] WEEK 03 solutions
2 parents b563904 + b5a8f86 commit 269c967

File tree

3 files changed

+152
-0
lines changed

3 files changed

+152
-0
lines changed

combination-sum/Blossssom.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @param - candidates: 정수 배열, target: 대상 정수
3+
* @returns - 선택한 숫자의 합이 해당하는 모든 고유 조합 반환
4+
* @description
5+
* 1. 요소는 여러번 사용 가능
6+
*/
7+
8+
function combinationSum(candidates: number[], target: number): number[][] {
9+
const result: number[][] = [];
10+
11+
function recursive(remain: number, idx: number, path: number[]) {
12+
if (remain === 0) {
13+
result.push([...path]);
14+
return;
15+
}
16+
17+
for (let i = idx; i < candidates.length; i++) {
18+
const currentNum = candidates[i];
19+
if (currentNum > remain) {
20+
break;
21+
}
22+
23+
path.push(currentNum);
24+
recursive(remain - currentNum, i, path);
25+
path.pop();
26+
}
27+
}
28+
29+
recursive(target, 0, []);
30+
return result;
31+
}
32+
33+
const candidates = [2, 3, 5];
34+
const target = 8;
35+
combinationSum(candidates, target);
36+

number-of-1-bits/Blossssom.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @param n - 양의 정수
3+
* @returns - 이진 변환 시 1의 갯수
4+
* @description
5+
* - 풀이 1: 직접 이진수로 변환하는 작업을 추가
6+
* - 이진 수 변환 후 배열 변경, 1만 뽑아내서 길이 반환하기
7+
* - 시간복잡도: O(n), 공간복잡도: O(n)
8+
*/
9+
10+
// function hammingWeight(n: number): number {
11+
// if (n === 0) return 0;
12+
// let result = "";
13+
// while (n > 0) {
14+
// result = (n % 2) + result;
15+
// n = Math.floor(n / 2);
16+
// }
17+
// let count = 0;
18+
// for (const ch of result) {
19+
// if (ch === "1") {
20+
// count++;
21+
// }
22+
// }
23+
// return count;
24+
// }
25+
26+
function hammingWeight(n: number): number {
27+
const bit = n
28+
.toString(2)
29+
.split("")
30+
.filter((v) => v === "1");
31+
32+
return bit.length;
33+
}
34+
35+
const n = 2147483645;
36+
37+
hammingWeight(n);
38+
39+

valid-palindrome/Blossssom.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* @param s - 입력 문자열
3+
* @returns - 입력 문자열이 palindrome 인지 반환
4+
* @description
5+
* 풀이 1 - 시간복잡도: O(n), 공간복잡도: O(n)
6+
* 풀이 2 - 시간복잡도: O(n), 공간복잡도: O(1) - 초반 정규식 스캔 및 소문자 변경이 한번에 일어나지 않고, 조기 종료
7+
* 풀이 3 - 1번과 복잡도는 같지만 split으로 실제 실행시간은 훨씬 빠르게 진행
8+
*/
9+
// function isPalindrome(s: string): boolean {
10+
// const clearString = s.replace(/[^a-zA-Z0-9]/g, "").toLocaleLowerCase();
11+
// let strLen = clearString.length - 1;
12+
// if (!clearString.length) {
13+
// return true;
14+
// }
15+
16+
// for (let i = 0; i < Math.floor(clearString.length / 2); i++) {
17+
// if (clearString[i] !== clearString[strLen]) {
18+
// return false;
19+
// }
20+
// strLen--;
21+
// }
22+
// return true;
23+
// }
24+
25+
// function isPalindrome(s: string): boolean {
26+
// let i = 0;
27+
// let j = s.length - 1;
28+
29+
// while (i < j) {
30+
// if (!isAlphaNum[s[i]]) {
31+
// i++;
32+
// continue;
33+
// }
34+
35+
// if (!isAlphaNum[s[j]]) {
36+
// j--;
37+
// continue;
38+
// }
39+
40+
// if (s[i].toLocaleLowerCase() !== s[j].toLocaleLowerCase()) {
41+
// return false;
42+
// }
43+
44+
// i++;
45+
// j--;
46+
// }
47+
// return true;
48+
// }
49+
50+
// function isAlphaNum(str: string): boolean {
51+
// const charC = str.charCodeAt(0);
52+
// return (
53+
// (charC >= "0".charCodeAt(0) && charC <= "9".charCodeAt(0)) ||
54+
// (charC >= "A".charCodeAt(0) && charC <= "Z".charCodeAt(0)) ||
55+
// (charC >= "a".charCodeAt(0) && charC <= "z".charCodeAt(0))
56+
// );
57+
// }
58+
59+
function isPalindrome(s: string): boolean {
60+
const cleaned = s
61+
.toLowerCase()
62+
.replace(/[^a-zA-Z0-9]/g, "")
63+
.split("");
64+
let left = 0;
65+
let right = cleaned.length - 1;
66+
while (left < right) {
67+
if (cleaned[left] === cleaned[right]) {
68+
left++;
69+
right--;
70+
} else {
71+
return false;
72+
}
73+
}
74+
return true;
75+
}
76+
77+

0 commit comments

Comments
 (0)