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
27 changes: 27 additions & 0 deletions combination-sum/jun0811.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* @param {number[]} candidates
* @param {number} target
* @return {number[][]}
*/
var combinationSum = function (candidates, target) {
const result = [];
candidates.sort((a, b) => a - b);
function backTracking(start, arr, total) {
if (total == target) {
Copy link
Contributor

Choose a reason for hiding this comment

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

total과 target을 === 연산자로도 비교할 수 있을 것 같습니다! ==연산자를 사용한 이유가 궁금합니다☺️

Copy link
Contributor Author

Choose a reason for hiding this comment

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

알고리즘 문제를 풀때는 엄격한 비교가 방해될때가 있어서 그렇게 하는 편인데.. 안좋은 습관일까요

result.push([...arr]);
return;
}

for (let i = start; i < candidates.length; i++) {
const cur = candidates[i];
if (cur + total > target) {
return;
}

backTracking(i, [...arr, cur], total + cur);
}
}

backTracking(0, [], 0);
return result;
};
32 changes: 32 additions & 0 deletions decode-ways/jun0811.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* @param {string} s
* @return {number}
*/

function check(str) {
if (str.length == 2) {
if (str[0] == '0') return false;
}
if (str >= 1 && str <= 26) return true;
return false;
}

var numDecodings = function (s) {
if (s[0] == '0') return 0;
const dp = [1, 1];

for (let i = 2; i <= s.length; i++) {
let tmp = dp[i - 1] + dp[i - 2];

// 2글자 체크 -> dp[i-2]을 뺴줌
const two_c = String(s[i - 2]) + String(s[i - 1]);
if (!check(two_c)) tmp -= dp[i - 2];

// 1글자 체크 -> dp[i-1]을 빼줌
if (!check(s[i - 1])) tmp -= dp[i - 1];

if (tmp == 0) return 0;
dp[i] = tmp;
}
return dp[s.length];
};
15 changes: 15 additions & 0 deletions maximum-subarray/jun0811.js
Copy link
Contributor

Choose a reason for hiding this comment

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

dp 배열을 사용해 문제를 풀이해주셨군요! 배열을 사용하지 않고 변수만 사용해서 공간복잡도를 줄이는 방법도 있습니다. 다음에 한 번 고려해보면 좋을 것 같습니다.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

넵 감사합니다

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* @param {number[]} nums
* @return {number}
*/
var maxSubArray = function (nums) {
const dp = [nums[0]];
for (let i = 1; i < nums.length; i++) {
const cur = nums[i];
if (dp[i - 1] < 0) dp[i] = cur;
else {
dp[i] = dp[i - 1] + cur;
}
}
return Math.max(...dp);
};
3 changes: 3 additions & 0 deletions number-of-1-bits/jun0811.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
* @param {number} n
* @return {number}
*/

// 1주차에 풀이 후 제출..

var hammingWeight = function (n) {
let res = 0;
while (n > 0) {
Expand Down
32 changes: 32 additions & 0 deletions valid-palindrome/jun0811.js
Copy link
Contributor

Choose a reason for hiding this comment

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

isAlphaNumeric() 이라는 함수를 정의하니 코드가 분리돼서 보기 편했습니다. 아스키코드로 풀이를 하셨네요! 정규표현식을 사용해서도 풀이할 수 있을 거 같습니다. 다음에 고려해보시면 좋을 것 같습니다.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

감사합니다. 정규표현식이 익숙하지 않아서 그냥 했는데 익숙하게 만들어야겠네요

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
var isPalindrome = function (s) {
let left = 0;
let right = s.length - 1;

while (left < right) {
while (left < right && !isAlphaNumeric(s[left])) {
left++;
}

while (left < right && !isAlphaNumeric(s[right])) {
right--;
}

if (s[left].toLowerCase() !== s[right].toLowerCase()) {
return false;
}

left++;
right--;
}

return true;
};

function isAlphaNumeric(char) {
const code = char.charCodeAt(0);
return (
(code >= 48 && code <= 57) || // 0-9
(code >= 65 && code <= 90) || // A-Z
(code >= 97 && code <= 122)
); // a-z
}