-
-
Notifications
You must be signed in to change notification settings - Fork 247
[youngduck] WEEK 03 solutions #1781
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* @param {number[]} candidates | ||
* @param {number} target | ||
* @return {number[][]} | ||
*/ | ||
var combinationSum = function (candidates, target) { | ||
// 작은숫자 부터 더해서, 더 큰 숫자를 더해볼필요없이 미리 리턴시키기위한용도, 중복값도 방지가능 | ||
candidates.sort(); | ||
const result = []; | ||
|
||
const sumArr = (arr) => { | ||
if (arr.length === 0) return 0; | ||
else { | ||
const sum = arr.reduce((acc, cur) => acc + cur); | ||
|
||
return sum; | ||
} | ||
}; | ||
|
||
const backtrack = (targetArr, startIndex) => { | ||
const sumData = sumArr(targetArr); | ||
|
||
if (sumData === target) { | ||
result.push([...targetArr]); | ||
return; | ||
} | ||
|
||
for (let i = startIndex; i < candidates.length; i++) { | ||
if (sumData < target) { | ||
backtrack([...targetArr, candidates[i]], i); | ||
} else if (sumData > target) { | ||
continue; | ||
} | ||
} | ||
}; | ||
|
||
// 백트랙킹 방식으로 풀예정 (재귀) | ||
// target보다 작을경우 계속 더함. 클경우 재귀멈춤 backtrack인수에 | ||
// targetArr만줬더니 result에 중복값이 생김. ex) 7을만들때[2,2,3],[2,3,2]같은 중복생김 | ||
// 자기자신 이상의 index를 타게하기위해서 startIndex | ||
backtrack([], 0); | ||
|
||
return result; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
* @param {number} n | ||
* @return {number} | ||
*/ | ||
var hammingWeight = function (n) { | ||
// 이진수 변환함수 시간복잡도: O(1) | ||
const bin = n.toString(2); | ||
|
||
// replace, replaceAll 시간복잡도: O(n) | ||
const result = bin.replaceAll('0', '').length; | ||
|
||
// 시간복잡도: O(n), 공간복잡도: O(1) | ||
|
||
return result; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* @param {string} s | ||
* @return {boolean} | ||
*/ | ||
var isPalindrome = function (s) { | ||
// 1. toLowerCase() - O(n) | ||
// 2. replace(/[^a-z0-9]/g, '') - O(n) | ||
const clean = s.toLowerCase().replace(/[^a-z0-9]/g, ''); | ||
|
||
// 3. [...clean] 스프레드 연산자 - O(n) | ||
// 4. reverse() - O(n) | ||
// 5. join('') - O(n) | ||
const reverse = [...clean].reverse().join(''); | ||
|
||
// 6. 문자열 비교 - O(n) | ||
return clean === reverse; | ||
}; | ||
|
||
// 시간복잡도 O(n) | ||
// 공간복잡도 O(n) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
오 replace 생각하지 못했는데 좋네용! 그리구 이진이라서 O(logn)이나.. 심지어 O(1) 으로도 풀이가 가능하더라구요. 다른 풀이들도 도전해보시면 좋을 것 같습니다.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
내장 함수를 잘 사용해 주셨네요! 비트 연산으로도 풀어보시면 어떤 언어에서도 풀이 가능하니 도전해보시면 좋을것 같습니다!