-
-
Notifications
You must be signed in to change notification settings - Fork 304
[leehyeyun] WEEK 02 solutions #2068
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
+166
−0
Merged
Changes from all commits
Commits
Show all changes
2 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,74 @@ | ||
| /** | ||
| * @param {number} n | ||
| * @return {number} | ||
| */ | ||
| /* | ||
| 계단을 올라가고 있을 때, | ||
| 꼭대기에 도달하려면 총 n 계단을 올라가야 한다. | ||
|
|
||
| 한 번에 오를 수 있는 계단 수는 | ||
| - 1계단 | ||
| - 2계단 | ||
| 두 가지뿐이다. | ||
|
|
||
| 이때, 정확히 n번째 계단(꼭대기)에 도달하는 | ||
| 서로 다른 방법의 수를 반환하는 함수. | ||
|
|
||
| 요청형식 : climbStairs(n) | ||
|
|
||
| 입력형식 : | ||
| - n은 정수 | ||
| - 1 <= n <= 45 | ||
|
|
||
| 출력형식 : | ||
| - n번째 계단에 도달할 수 있는 서로 다른 방법의 수 (정수) | ||
|
|
||
| 예시 : | ||
|
|
||
| Example 1 | ||
| 입력 : | ||
| n = 2 | ||
| 출력 : | ||
| 2 | ||
| 설명 : | ||
| 1) 1계단 + 1계단 | ||
| 2) 2계단 | ||
|
|
||
| Example 2 | ||
| 입력 : | ||
| n = 3 | ||
| 출력 : | ||
| 3 | ||
| 설명 : | ||
| 1) 1계단 + 1계단 + 1계단 | ||
| 2) 1계단 + 2계단 | ||
| 3) 2계단 + 1계단 | ||
|
|
||
| 제약사항 : | ||
| - 1 <= n <= 45 | ||
|
|
||
| 참고 : | ||
| - 각 계단에 도달하는 "방법의 수"를 잘 관찰하면 | ||
| 일정한 규칙(수열)이 나타난다. | ||
| */ | ||
| var climbStairs = function(n) { | ||
|
|
||
| const map = new Map(); | ||
|
|
||
| map.set(1,1); | ||
| map.set(2,2) | ||
|
|
||
| for (var stairs = 3; stairs <= n; stairs ++) | ||
| { | ||
| var stairs_hap = map.get(stairs-1) + map.get(stairs-2) | ||
| map.set(stairs,stairs_hap) | ||
| } | ||
|
|
||
| var result = map.get(n); | ||
|
|
||
| return result; | ||
| }; | ||
|
|
||
| console.log(climbStairs(2)); //2 | ||
| console.log(climbStairs(3)); //3 | ||
|
|
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,92 @@ | ||
| /** | ||
| * @param {string} s | ||
| * @param {string} t | ||
| * @return {boolean} | ||
| */ | ||
|
|
||
| /* | ||
| 두 문자열 s와 t가 주어졌을 때, | ||
| t가 s의 애너그램(anagram)이면 true, | ||
| 아니면 false를 반환하는 함수. | ||
|
|
||
| 애너그램이란? | ||
| → 문자열에 포함된 문자들의 종류와 개수가 모두 동일한 경우. | ||
| (순서는 상관 없음) | ||
|
|
||
| 요청형식 : isAnagram(s, t) | ||
|
|
||
| 입력형식 : | ||
| - s, t는 모두 소문자 알파벳으로 구성된 문자열 | ||
| - 1 <= s.length, t.length <= 5 * 10^4 | ||
|
|
||
| 출력형식 : | ||
| - t가 s의 애너그램이면 true | ||
| - 아니면 false | ||
|
|
||
| 요청예시 : | ||
| isAnagram("anagram", "nagaram") | ||
| 출력예시 : | ||
| true | ||
|
|
||
| isAnagram("rat", "car") | ||
| 출력예시 : | ||
| false | ||
|
|
||
| Follow-up : | ||
| - 만약 유니코드 문자(한글, 일본어, 이모지 등)를 포함한다면 | ||
| 문자 빈도수를 저장할 때 ASCII 배열 대신 | ||
| 해시맵(Map, Object)을 사용하여 해결할 수 있다. | ||
| */ | ||
|
|
||
| var isAnagram = function(s, t) { | ||
|
|
||
| const map = new Map(); | ||
|
|
||
| //문자열 s를 순회하면서 각 요소를 map 방식으로 넣어줌 | ||
| for(const s_char of s){ | ||
| if(map.has(s_char)) | ||
| { | ||
| var s_char_count = map.get(s_char) | ||
|
|
||
| s_char_count ++; | ||
|
|
||
| map.set(s_char,s_char_count) | ||
| }else { | ||
| map.set(s_char,1) | ||
| } | ||
| } | ||
|
|
||
| //문자열 t를 순회하면서 각 요소를 map에서 빼줌 | ||
| for(const t_char of t){ | ||
| if(map.has(t_char)) | ||
| { | ||
| var t_char_count = map.get(t_char) | ||
|
|
||
| t_char_count --; | ||
|
|
||
| if(t_char_count === 0) | ||
| { | ||
| map.delete(t_char) | ||
| }else if(t_char_count > 0) | ||
| { | ||
| map.set(t_char,t_char_count) | ||
| }else { | ||
| return false; | ||
| } | ||
| }else { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| //최종 리턴 | ||
| if(map.size == 0) | ||
| { | ||
| return true; | ||
| }else { | ||
| return false; | ||
| } | ||
| }; | ||
|
|
||
| console.log("Example 1:", isAnagram("anagram", "nagaram")); // true | ||
| console.log("Example 2:", isAnagram("rat", "car")); // false | ||
|
|
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.
JS 개발자가 아닌데도 가독성이 좋게 읽혔습니다.
예시 및 Sample도 첨부해주셔서 이해가 더 잘됐던 것 같습니다