-
-
Notifications
You must be signed in to change notification settings - Fork 304
[leehyeyun] WEEK 03 solutions #2108
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
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,72 @@ | ||
| /** | ||
| * @param {number} n | ||
| * @return {number} | ||
| */ | ||
| /* | ||
| 양의 정수 n이 주어졌을 때, | ||
| 이 정수를 2진수(binary)로 변환했을 때 | ||
| '1'로 설정된 비트(set bit)의 개수를 구하는 함수. | ||
|
|
||
| 이 값은 ‘해밍 가중치(Hamming Weight)’라고도 부른다. | ||
|
|
||
| 요청 형식 : hammingWeight(n) | ||
|
|
||
| 입력 형식 : | ||
| - n은 양의 정수 | ||
| - 1 <= n <= 2^31 - 1 | ||
|
|
||
| 출력 형식 : | ||
| - n의 이진 표현에서 '1'의 개수 (정수) | ||
|
|
||
| 예시 : | ||
|
|
||
| Example 1 | ||
| 입력 : | ||
| n = 11 | ||
| 출력 : | ||
| 3 | ||
| 설명 : | ||
| 11 → 1011 (2진수) | ||
| 1이 총 3개 | ||
|
|
||
| Example 2 | ||
| 입력 : | ||
| n = 128 | ||
| 출력 : | ||
| 1 | ||
| 설명 : | ||
| 128 → 10000000 | ||
| 1이 하나뿐 | ||
|
|
||
| Example 3 | ||
| 입력 : | ||
| n = 2147483645 | ||
| 출력 : | ||
| 30 | ||
| 설명 : | ||
| 2147483645 → 1111111111111111111111111111101 | ||
| 1이 총 30개 | ||
|
|
||
| 제약사항 : | ||
| - 매 호출이 빠르게 동작해야 함 | ||
| - 팁: 비트를 하나씩 확인하는 반복문 or | ||
| n &= (n - 1) 같은 비트 최적화가 존재함 | ||
|
|
||
| 참고 : | ||
| - 만약 이 함수를 반복 호출해야 한다면, | ||
| 사전 계산된 lookup table을 사용하는 방식으로 | ||
| 추가 최적화할 수 있다. | ||
|
|
||
| */ | ||
| var hammingWeight = function(n) { | ||
|
|
||
| let binaryString = n.toString(2); | ||
| let value = binaryString.split("").filter(x => x === "1").length; | ||
|
|
||
| return value; | ||
| }; | ||
|
|
||
| console.log(hammingWeight(11)); | ||
| console.log(hammingWeight(128)); | ||
| console.log(hammingWeight(2147483645)); | ||
|
|
||
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,76 @@ | ||
| /** | ||
| * @param {string} s | ||
| * @return {boolean} | ||
| */ | ||
| /* | ||
| 문자열이 주어졌을 때, | ||
| 이 문자열이 '유효한 회문(palindrome)'인지 판별하는 함수. | ||
|
|
||
| 회문 판별 규칙: | ||
| - 대문자는 소문자로 변환한다. | ||
| - 영문자(a~z)와 숫자(0~9)만 남기고 나머지 문자는 제거한다. | ||
| - 정제된 문자열을 앞에서 읽은 것과 뒤에서 읽은 것이 같으면 회문이다. | ||
|
|
||
| 요청 형식 : isPalindrome(s) | ||
|
|
||
| 입력 형식 : | ||
| - s는 문자열(String) | ||
| - 1 <= s.length <= 2 * 10^5 | ||
| - 문자열은 ASCII 출력 문자로만 구성됨 | ||
|
|
||
| 출력 형식 : | ||
| - 유효한 회문이면 true | ||
| - 아니면 false | ||
|
|
||
| 예시 : | ||
|
|
||
| Example 1 | ||
| 입력 : | ||
| s = "A man, a plan, a canal: Panama" | ||
| 출력 : | ||
| true | ||
| 설명 : | ||
| 정제하면 "amanaplanacanalpanama" | ||
| 회문이므로 true | ||
|
|
||
| Example 2 | ||
| 입력 : | ||
| s = "race a car" | ||
| 출력 : | ||
| false | ||
| 설명 : | ||
| 정제하면 "raceacar" | ||
| 회문이 아님 | ||
|
|
||
| Example 3 | ||
| 입력 : | ||
| s = " " | ||
| 출력 : | ||
| true | ||
| 설명 : | ||
| 정제 후 "" (빈 문자열) | ||
| 빈 문자열은 회문으로 간주됨 | ||
|
|
||
| 제약사항 : | ||
| - 문자열의 길이가 매우 크므로 O(n) 방식이 적합함 | ||
| */ | ||
| var isPalindrome = function(s) { | ||
|
|
||
| let cleanString = s.toLowerCase().replace(/[^a-z0-9]/g, ''); | ||
|
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. 확실히 정규식으로 제거해 버리는게 깔끔하긴 하네요! |
||
|
|
||
| let splitString = cleanString.split(""); | ||
| let reverseArray = splitString.reverse(); | ||
| let joinArray = reverseArray.join(""); | ||
|
|
||
| if(cleanString != joinArray) | ||
| { | ||
| return false | ||
| }else { | ||
| return true | ||
| } | ||
| }; | ||
|
|
||
| console.log(isPalindrome("A man, a plan, a canal: Panama")); | ||
| console.log(isPalindrome("race a car")); | ||
| console.log(isPalindrome(" ")); | ||
|
|
||
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.
바로 이진수로 변환해서 1만 추려내는 방식이군요!
저도 이와 비슷하게 풀었습니다!!
혹시 let을 사용한 이유가 있을까요?
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.
안녕하세요, 좋은 피드백 감사합니다 ㅎㅎ
사실 저 부분은 깊게 생각하지 않고 let을 사용했는데,
값이 재할당되지 않으니 const가 더 적절하겠네요 . ㅎㅎ
다음부터는 변수 성격에 맞게 선언할 수 있도록 개선하겠습니다. 감사합니다 :)