Skip to content

Commit 4a16556

Browse files
authored
Merge pull request #1809 from suhyenim/5th/week03
[suhyenim] WEEK03 solutions
2 parents 421acfc + 9f6577b commit 4a16556

File tree

2 files changed

+136
-0
lines changed

2 files changed

+136
-0
lines changed

number-of-1-bits/suhyenim.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/* [5th/week03] 191. Number of 1 Bits
2+
3+
1. 문제 요약
4+
링크: https://leetcode.com/problems/number-of-1-bits/description/
5+
주어진 수를 이진수로 만들었을 때, 1의 개수 반환
6+
7+
2. 문제 풀이
8+
제출1: 2로 나눈 나머지로 배열 만들고 -> 해당 배열에서 1의 개수 계산해서 반환
9+
성공: 시간 복잡도는 O(logn), 공간 복잡도는 O(logn)
10+
=> Time: 1 ms (16.28%), Space: 41.3 MB (7.99%)
11+
12+
class Solution {
13+
public int hammingWeight(int n) {
14+
List<Integer> rr = new ArrayList<>();
15+
while (n > 0) {
16+
int q = n / 2;
17+
int r = n % 2;
18+
rr.add(r);
19+
n = q;
20+
}
21+
int count = 0;
22+
for (int i = 0; i < rr.size(); i++) {
23+
if (rr.get(i) == 1) {
24+
count++;
25+
}
26+
}
27+
return count;
28+
}
29+
}
30+
31+
*/
32+
33+
class Solution {
34+
public int hammingWeight(int n) {
35+
List<Integer> rr = new ArrayList<>();
36+
while (n > 0) {
37+
int q = n / 2;
38+
int r = n % 2;
39+
rr.add(r);
40+
n = q;
41+
}
42+
int count = 0;
43+
for (int i = 0; i < rr.size(); i++) {
44+
if (rr.get(i) == 1) {
45+
count++;
46+
}
47+
}
48+
return count;
49+
}
50+
}

valid-palindrome/suhyenim.java

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/* [5th/week03] 125. Valid Palindrome
2+
3+
1. 문제 요약
4+
링크: https://leetcode.com/problems/valid-palindrome/description/
5+
주어진 문자열에서 대->소문자 & 숫자만 남겼을 때, 앞으로 읽어도 뒤에서 읽어도 동일한 문자열이면 true 반환
6+
7+
2. 문제 풀이
8+
제출1: 소문자와 숫자만 남긴 새로운 문자열 만들고 -> 양끝에서 안쪽으로 문자 비교
9+
성공: 시간 복잡도는 O(n), 공간 복잡도는 O(n)
10+
=> Time: 2 ms (98.53%), Space: 45.3 MB (19.59%)
11+
12+
class Solution {
13+
public boolean isPalindrome(String s) {
14+
List<Integer> arr = new ArrayList<>();
15+
for (char c : s.toCharArray()) {
16+
if (c >= 'A' && c <= 'Z') {
17+
c = (char)(c + 32);
18+
arr.add((int)c);
19+
}
20+
else if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')) {
21+
arr.add((int)c);
22+
}
23+
}
24+
for (int i = 0; i < arr.size() / 2; i++) {
25+
if (arr.get(i) != arr.get(arr.size() - 1 - i)) {
26+
return false;
27+
}
28+
}
29+
return true;
30+
}
31+
}
32+
33+
풀이2: 제출1과 로직 동일하지만 공간 복잡도 낮춤 (새로운 문자열 생성 안하고 진행하기 때문)
34+
성공: 시간 복잡도는 O(n), 공간 복잡도는 O(1)
35+
=> Time: 2 ms (98.59%), Space: 45.4 MB (14.48%)
36+
37+
class Solution {
38+
public boolean isPalindrome(String s) {
39+
int low = 0;
40+
int high = s.length() - 1;
41+
while (low < high) {
42+
while (low < high && !Character.isLetterOrDigit(s.charAt(low))) {
43+
low++;
44+
}
45+
while (low < high && !Character.isLetterOrDigit(s.charAt(high))) {
46+
high--;
47+
}
48+
if (Character.toLowerCase(s.charAt(low)) != Character.toLowerCase(s.charAt(high))) {
49+
return false;
50+
}
51+
low++;
52+
high--;
53+
}
54+
return true;
55+
}
56+
}
57+
58+
59+
3. TIL
60+
아스키 코드 정리
61+
- 대문자: A(65) ~ Z(90)
62+
- 소문자: a(97) ~ z(122)
63+
- 숫자: 0(48) ~ 9(57)
64+
65+
*/
66+
67+
class Solution {
68+
public boolean isPalindrome(String s) {
69+
List<Integer> arr = new ArrayList<>();
70+
for (char c : s.toCharArray()) {
71+
if (c >= 'A' && c <= 'Z') {
72+
c = (char)(c + 32);
73+
arr.add((int)c);
74+
}
75+
else if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')) {
76+
arr.add((int)c);
77+
}
78+
}
79+
for (int i = 0; i < arr.size() / 2; i++) {
80+
if (arr.get(i) != arr.get(arr.size() - 1 - i)) {
81+
return false;
82+
}
83+
}
84+
return true;
85+
}
86+
}

0 commit comments

Comments
 (0)