Skip to content

Commit 429014b

Browse files
authored
Merge pull request #2454 from grapefruit13/main
2 parents 00579e2 + 87b023c commit 429014b

3 files changed

Lines changed: 56 additions & 0 deletions

File tree

combination-sum/grapefruit13.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function combinationSum(candidates: number[], target: number): number[][] {
2+
const answer: number[][] = [];
3+
4+
function dfs(index: number, path: number[], sum: number) {
5+
if (sum === target) {
6+
answer.push([...path]);
7+
return;
8+
}
9+
if (sum > target) return;
10+
11+
for (let i = index; i < candidates.length; i++) {
12+
path.push(candidates[i]);
13+
dfs(i, path, sum + candidates[i]);
14+
path.pop();
15+
}
16+
}
17+
18+
dfs(0, [], 0);
19+
return answer;
20+
}

decode-ways/grapefruit13.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function numDecodings(s: string): number {
2+
if (s[0]==="0") return 0;
3+
4+
const dp = new Array(s.length).fill(0);
5+
dp[0] = 1;
6+
7+
for (let i=1; i<s.length; i++) {
8+
if (s[i] !== "0") {
9+
dp[i] += dp[i-1];
10+
}
11+
12+
const two = Number(s[i-1] + s[i]);
13+
14+
if (10 <= two && two <= 26) {
15+
if (i === 1) {
16+
dp[i] += 1;
17+
} else {
18+
dp[i] += dp[i-2];
19+
}
20+
}
21+
}
22+
return dp[s.length - 1];
23+
};

valid-palindrome/grapefruit13.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function isPalindrome(s: string): boolean {
2+
const cleaned = s.toLowerCase().replace(/[^a-z0-9]/g, '');
3+
4+
let left = 0;
5+
let right = cleaned.length - 1;
6+
7+
while (left < right) {
8+
if (cleaned[left] !== cleaned[right]) return false;
9+
left++;
10+
right--;
11+
}
12+
return true;
13+
}

0 commit comments

Comments
 (0)