-
-
Notifications
You must be signed in to change notification settings - Fork 304
[acious] WEEK 03 solutions #2082
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 3 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,19 @@ | ||
| class Solution { | ||
| fun hammingWeight(n: Int): Int { | ||
| // 2^31 = 1,073,741,823 | ||
| // 몫 : dividend | ||
| // 한텀당 n을 2로 나눠서 몫과 나머지를 구해냄. 나머지가 1이면 result에 +=1, 나머지가 0이면 result에 변화없음. | ||
| // 몫이 0으로 떨어지면 연산이 끝나고 result 반환 | ||
| // 시간 복잡도 : n이 최대 2^31이고 n이 커짐에 따라 최대 31번 반복문이 도므로 O(1) | ||
| // 공간복잡도 : O(1) | ||
| var dividend = n | ||
| var result = 0 | ||
| while (dividend != 0) { | ||
| if (dividend % 2 == 1) { | ||
| result+=1 | ||
| } | ||
| dividend = dividend/2 | ||
| } | ||
| 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,43 @@ | ||
| class Solution { | ||
| fun isPalindrome(s: String): Boolean { | ||
| // 문장의 길이는 최대 20만 | ||
| // 문자열이 공백이면 return true | ||
| // for-loop 한턴에 양쪽 끝에서부터 시작. 양쪽 index가 서로 교차되면 return true | ||
| // 각각 char이 alphanumeric이 아니면 한칸 더이동. | ||
| // 영문 대문자면 영문 소문자로 치환. | ||
| // - 양쪽 끝의 char를 비교해서 같으면 양쪽 index를 이동시킴. | ||
| // - 양쪽 끝의 char를 비교해서 같지않으면 return false | ||
| // 시간복잡도 : O(n/2) | ||
| // 공간복잡도 : O(1) | ||
| if (s.isEmpty()) { | ||
| return true | ||
| } | ||
|
|
||
| var left = 0 | ||
| var right = s.length - 1 | ||
|
|
||
| while (left <= right) { // 서로 교차하기 전까지만 비교하면 됨 (<= 대신 < 사용 가능) | ||
|
|
||
| // 1. Char가 영문자나 숫자가 아닌지 확인 | ||
| if (!s[left].isLetterOrDigit()) { | ||
| left += 1 | ||
| continue | ||
| } | ||
|
|
||
| if (!s[right].isLetterOrDigit()) { | ||
| right -= 1 | ||
| continue | ||
| } | ||
|
|
||
| // 2. 대소문자 통일 후 비교 | ||
| if (s[left].lowercaseChar() != s[right].lowercaseChar()) { | ||
| return false | ||
| } else { | ||
| left += 1 | ||
| right -= 1 | ||
| } | ||
| } | ||
|
|
||
| return true | ||
| } | ||
| } | ||
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.
[참고 정보]
Two Pointer방식으로 풀면 메모리와 시간 효율적인 측면에서 좋습니다 👍알파벳과 숫자만 남기는 전처리 후 비교하는 방법도 시간 복잡도는 동일하면서 코드가 더 간결해질 수 있더라고요!
다만 메모리 효율성은 조금 떨어지는 Trade Off가 있습니다 ㅎㅎ