Skip to content

[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 1 commit into from
Aug 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions number-of-1-bits/suhyenim.java
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;
}
}
86 changes: 86 additions & 0 deletions valid-palindrome/suhyenim.java
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;
}
}