-
-
Notifications
You must be signed in to change notification settings - Fork 247
[suhyenim] WEEK03 solutions #1809
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
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,50 @@ | ||
/* [5th/week03] 191. Number of 1 Bits | ||
|
||
1. 문제 요약 | ||
링크: https://leetcode.com/problems/number-of-1-bits/description/ | ||
주어진 수를 이진수로 만들었을 때, 1의 개수 반환 | ||
|
||
2. 문제 풀이 | ||
제출1: 2로 나눈 나머지로 배열 만들고 -> 해당 배열에서 1의 개수 계산해서 반환 | ||
성공: 시간 복잡도는 O(logn), 공간 복잡도는 O(logn) | ||
=> Time: 1 ms (16.28%), Space: 41.3 MB (7.99%) | ||
|
||
class Solution { | ||
public int hammingWeight(int n) { | ||
List<Integer> rr = new ArrayList<>(); | ||
while (n > 0) { | ||
int q = n / 2; | ||
int r = n % 2; | ||
rr.add(r); | ||
n = q; | ||
} | ||
int count = 0; | ||
for (int i = 0; i < rr.size(); i++) { | ||
if (rr.get(i) == 1) { | ||
count++; | ||
} | ||
} | ||
return count; | ||
} | ||
} | ||
|
||
*/ | ||
|
||
class Solution { | ||
public int hammingWeight(int n) { | ||
List<Integer> rr = new ArrayList<>(); | ||
while (n > 0) { | ||
int q = n / 2; | ||
int r = n % 2; | ||
rr.add(r); | ||
n = q; | ||
} | ||
int count = 0; | ||
for (int i = 0; i < rr.size(); i++) { | ||
if (rr.get(i) == 1) { | ||
count++; | ||
} | ||
} | ||
return count; | ||
} | ||
} |
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,86 @@ | ||
/* [5th/week03] 125. Valid Palindrome | ||
|
||
1. 문제 요약 | ||
링크: https://leetcode.com/problems/valid-palindrome/description/ | ||
주어진 문자열에서 대->소문자 & 숫자만 남겼을 때, 앞으로 읽어도 뒤에서 읽어도 동일한 문자열이면 true 반환 | ||
|
||
2. 문제 풀이 | ||
제출1: 소문자와 숫자만 남긴 새로운 문자열 만들고 -> 양끝에서 안쪽으로 문자 비교 | ||
성공: 시간 복잡도는 O(n), 공간 복잡도는 O(n) | ||
=> Time: 2 ms (98.53%), Space: 45.3 MB (19.59%) | ||
|
||
class Solution { | ||
public boolean isPalindrome(String s) { | ||
List<Integer> arr = new ArrayList<>(); | ||
for (char c : s.toCharArray()) { | ||
if (c >= 'A' && c <= 'Z') { | ||
c = (char)(c + 32); | ||
arr.add((int)c); | ||
} | ||
else if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')) { | ||
arr.add((int)c); | ||
} | ||
} | ||
for (int i = 0; i < arr.size() / 2; i++) { | ||
if (arr.get(i) != arr.get(arr.size() - 1 - i)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} | ||
|
||
풀이2: 제출1과 로직 동일하지만 공간 복잡도 낮춤 (새로운 문자열 생성 안하고 진행하기 때문) | ||
성공: 시간 복잡도는 O(n), 공간 복잡도는 O(1) | ||
=> Time: 2 ms (98.59%), Space: 45.4 MB (14.48%) | ||
|
||
class Solution { | ||
public boolean isPalindrome(String s) { | ||
int low = 0; | ||
int high = s.length() - 1; | ||
while (low < high) { | ||
while (low < high && !Character.isLetterOrDigit(s.charAt(low))) { | ||
low++; | ||
} | ||
while (low < high && !Character.isLetterOrDigit(s.charAt(high))) { | ||
high--; | ||
} | ||
if (Character.toLowerCase(s.charAt(low)) != Character.toLowerCase(s.charAt(high))) { | ||
return false; | ||
} | ||
low++; | ||
high--; | ||
} | ||
return true; | ||
} | ||
} | ||
|
||
|
||
3. TIL | ||
아스키 코드 정리 | ||
- 대문자: A(65) ~ Z(90) | ||
- 소문자: a(97) ~ z(122) | ||
- 숫자: 0(48) ~ 9(57) | ||
|
||
*/ | ||
|
||
class Solution { | ||
public boolean isPalindrome(String s) { | ||
List<Integer> arr = new ArrayList<>(); | ||
for (char c : s.toCharArray()) { | ||
if (c >= 'A' && c <= 'Z') { | ||
c = (char)(c + 32); | ||
arr.add((int)c); | ||
} | ||
else if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')) { | ||
arr.add((int)c); | ||
} | ||
} | ||
for (int i = 0; i < arr.size() / 2; i++) { | ||
if (arr.get(i) != arr.get(arr.size() - 1 - i)) { | ||
return false; | ||
} | ||
} | ||
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.
Uh oh!
There was an error while loading. Please reload this page.