-
-
Notifications
You must be signed in to change notification settings - Fork 305
[yuhyeon99] WEEK 03 solutions #2090
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
5 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,32 @@ | ||
| /** | ||
| * 각각 다른 정수 배열 candidates와, 목표 정수 target이 주어진다. | ||
| * 반환하라, 타겟과 값이 같은 합을 보유한 고유한 조합을. | ||
| * 조합은 어떤 순서(any order)로든 반환할 수 있습니다. | ||
| * 같은 숫자를 여러 조합에 무제한(unlimited number of times) 쓸 수 있습니다. | ||
| * 선택한 숫자 중 적어도 하나의 빈도가 다르다면 두 조합은 고유합니다. | ||
| * target은 150보다 낮은 숫자입니다. | ||
| * | ||
| * @param {number[]} candidates | ||
| * @param {number} target | ||
| * @return {number[][]} | ||
| */ | ||
| var combinationSum = function(candidates, target) { | ||
| var result = []; | ||
| var nums = []; | ||
| function dfs(start, total) { | ||
| if (total > target) return; | ||
| if (total === target) result.push(nums.slice()); | ||
|
|
||
|
|
||
| for (let i = start; i < candidates.length; i ++) { | ||
| let num = candidates[i]; | ||
| nums.push(num); | ||
| dfs(i, total + num); | ||
| nums.pop(); | ||
| } | ||
|
|
||
| } | ||
| dfs(0, 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,36 @@ | ||
| /** | ||
| * 인코드 된 메세지를 디코드(decode)하려면, 맵핑의 규칙을 반대로 적용하여 숫자를 다시 알파벳 대문자로 돌려놔야 한다. | ||
| * 예를 들어, 숫자 11106은 1 1 10 6으로 분할하면 AAJF로 디코드할 수도 있고, 11 10 6으로 분할하면 KJF로도 디코드할 | ||
| 수 있다. | ||
| * 문자열 타입의 숫자가 주어졌을 때 알파벳 문자열로 디코드할 수 있는 방법의 개수를 구하라. | ||
| * @param {string} s | ||
| * @return {number} | ||
| */ | ||
| var numDecodings = function(s) { | ||
| const memo = new Map(); | ||
| memo.set(s.length, 1); | ||
|
|
||
| function dfs(start) { | ||
| if (memo.has(start)) { | ||
| return memo.get(start); | ||
| } | ||
|
|
||
| // 0으로 시작하면 해석 불가 | ||
| if (s[start] === "0") { | ||
| memo.set(start, 0); | ||
| return 0; | ||
| } | ||
|
|
||
| let count = dfs(start + 1); // 한 글자 해석 | ||
|
|
||
| // 두 글자 해석 가능한 경우 | ||
| if (start + 1 < s.length && parseInt(s.substring(start, start + 2)) <= 26) { | ||
| count += dfs(start + 2); | ||
| } | ||
|
|
||
| memo.set(start, count); | ||
| return count; | ||
| } | ||
|
|
||
| return dfs(0); | ||
| }; |
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,13 @@ | ||
| /** | ||
| * @param {number[]} nums | ||
| * @return {number} | ||
| */ | ||
| var maxSubArray = function(nums) { | ||
| let maxSum = nums[0]; | ||
| let sum = 0; | ||
| nums.forEach((num) => { | ||
| sum = Math.max(sum + num, num); | ||
| maxSum = Math.max(sum, maxSum); | ||
| }); | ||
| return maxSum; | ||
| }; |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 잘 풀어주셨습니다! 저도 좀 찾아보다가 이렇게도 풀기도 하는구나 싶은 좋은 참고자료 하나 발견하여 공유드립니다!
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 공유해주셔서 감사합니다! 이런 방법도 있군요 ㅎㅎ.. 잘 보겠습니다! |
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,12 @@ | ||
| /** | ||
| * @param {number} n | ||
| * @return {number} | ||
| */ | ||
| var hammingWeight = function(n) { | ||
| var answer = 0; | ||
| while(n >= 1) { | ||
| if (n % 2 === 1) answer ++; | ||
| n = Math.floor(n/2); | ||
| } | ||
| return answer; | ||
| }; |
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,11 @@ | ||
| /** | ||
| * @param {string} s | ||
| * @return {boolean} | ||
| */ | ||
| var isPalindrome = function(s) { | ||
| const regexp = /[^a-z0-9]/g; | ||
| s = s.toLowerCase(); | ||
| s = s.replaceAll(regexp, ""); | ||
|
|
||
| return s === [...s].reverse().join(''); | ||
| }; |
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.
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.
깔끔하게 잘 써주셔서 잘 읽혔습니다. 사소한 부분이긴 하지만 해당 부분에서 바로 return해도 되지 않을까 싶네요!