-
Notifications
You must be signed in to change notification settings - Fork 1
[WEEK08-1] 최준호 #34
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
[WEEK08-1] 최준호 #34
Changes from all commits
Commits
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,43 @@ | ||
| /** | ||
| * @param {string[]} strs | ||
| * @return {string} | ||
| */ | ||
| var longestCommonPrefix = function (strs) { | ||
| const checkChar = (strs, c, index) => { | ||
| return strs.every((str) => str[index] === c); | ||
|
Collaborator
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. 공통 접두사 여부도 every로 표현해서 의도가 잘 드러난 것 같아요! 👍 |
||
| }; | ||
|
|
||
| if (strs.length === 1) { | ||
| return strs[0]; | ||
| } | ||
|
|
||
| let answer = ""; | ||
|
|
||
| for (let i = 0; i < strs[0].length; i++) { | ||
| const char = strs[0][i]; | ||
|
|
||
| if (checkChar(strs, char, i)) { | ||
| answer += char; | ||
| } else { | ||
| break; | ||
|
Member
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. break말고 바로 answer를 반환해줘도 좋을 것 같아요. |
||
| } | ||
| } | ||
|
|
||
| return answer; | ||
| }; | ||
|
|
||
| /* | ||
| 14분 걸림. | ||
|
|
||
| 시간 복잡도는 O(N x M) N은 문자열의 개수, M은 공통 접두사를 확인하기 위해 보는 문자 수이다. | ||
| 공간 복잡도는 O(1)이다. answer 문자열의 길이는 공통 접두사의 길이까지 될 수 있기 때문이다. | ||
|
|
||
| strs 배열의 첫 번째 문자열을 기준으로 공통 접두사를 확인하는 방식으로 풀이했다. | ||
| checkChar 함수를 만들어서, strs 배열의 모든 문자열이 index 위치에서 char와 같은지 확인하는 방식으로 풀이했다. | ||
| strs 배열의 첫 번째 문자열을 순회하면서, checkChar 함수를 이용해서 공통 접두사를 확인했다. | ||
| 공통 접두사가 맞으면 answer에 char를 더해주고, 공통 접두사가 아니면 반복문을 종료했다. | ||
|
|
||
|
|
||
| 복쟙하게 생각하지 않고 주어진 조건에 대해 풀이했다. | ||
| strs 배열의 첫 번째 문자열을 기준으로 공통 접두사를 확인하는 방식이 가장 직관적인 풀이인 것 같다. | ||
| */ | ||
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 @@ | ||
| /** | ||
| * @param {number[]} nums | ||
| * @return {number} | ||
| */ | ||
| var singleNumber = function (nums) { | ||
| if (nums.length === 1) return nums[0]; | ||
|
|
||
| const map = new Map(); | ||
|
|
||
| nums.forEach((num) => { | ||
| map.set(num, map.has(num) ? map.get(num) + 1 : 1); | ||
| }); | ||
|
|
||
| for (const [k, v] of map) { | ||
| if (v === 1) { | ||
| return k; | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| /* | ||
| 8분 걸림. | ||
|
|
||
| 시간 복잡도는 O(n)이다. n은 입력 배열의 길이이다. | ||
| 공간 복잡도는 O(n)이다. Map 자료구조에 최대 n개의 요소가 저장될 수 있기 때문이다. | ||
|
|
||
| Map 자료구조를 이용하여 풀이했다. | ||
| 배열을 순회하면서, Map에 요소가 있는지 확인하고, 있으면 요소의 값을 1 증가시키고, 없으면 요소를 추가하고 값을 1로 설정했다. | ||
| Map을 순회하면서, 값이 1인 요소의 키를 반환했다. | ||
|
|
||
| Map을 이용해서 풀이를 했지만, 뭔가 for문을 한 번만 돌리는 풀이가 있을 것 같은 느낌이 들었다. | ||
| */ |
Oops, something went wrong.
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.
조건 검사를 분리 작성해서 가독성이 좋네요