Skip to content

Commit a00972b

Browse files
authored
Merge pull request #2435 from OstenHun/main
[OstenHun] WEEK 03 Solutions
2 parents d2532d5 + c055eff commit a00972b

2 files changed

Lines changed: 135 additions & 0 deletions

File tree

number-of-1-bits/OstenHun.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
191. Number of 1 Bits
3+
4+
Given a positive integer n, write a function
5+
that returns the number of set bits in its binary representation
6+
(also known as the Hamming weight).
7+
8+
Example 1:
9+
Input: n = 11
10+
Output: 3
11+
12+
Explanation:
13+
The input binary string 1011 has a total of three set bits.
14+
15+
Constraints:
16+
1 <= n <= 2^31 - 1
17+
*/
18+
19+
// 비트 연산을 이용하여 풀기
20+
#include <iostream>
21+
using namespace std;
22+
23+
// 시간 복잡도 : O(logn) -> n >> 1 연산을 하기 때문에 절반씩 줄어듬
24+
// 문제의 조건은 32bit 내의 범위이기 때문에 32번의 반복으로 항상 끝나기에 O(1)이라고 할 수 있다.
25+
// 공간 복잡도 : O(1)
26+
class Solution {
27+
public:
28+
int hammingweight(int n) {
29+
unsigned int answer = 0;
30+
while(n>0) {
31+
if (n & 1)
32+
answer++;
33+
n = n >> 1;
34+
}
35+
36+
// 처음 풀었던 풀이.
37+
// unsigned int answer = 0;
38+
// for (int i = 0; i < 32; i++) {
39+
// if ((n >> i) & 1) answer++;
40+
// }
41+
42+
// 생각 못 한 풀이
43+
// -> n & (n-1) 을 하면 가장 오른쪽 1비트를 지운다.
44+
// while (n > 0) {
45+
// n &= (n - 1);
46+
// answer++;
47+
// }
48+
49+
return answer;
50+
}
51+
};

valid-palindrome/OstenHun.cpp

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
A phrase is a palindrome if,
3+
after converting all uppercase letters into lowercase letters and
4+
removing all non-alphanumeric characters, it reads the same forward and backward.
5+
Alphanumeric characters include letters and numbers.
6+
7+
Given a string s, return true if it is a palindrome, or false otherwise.
8+
9+
Example 1:
10+
11+
Input: s = "A man, a plan, a canal: Panama"
12+
Output: true
13+
Explanation: "amanaplanacanalpanama" is a palindrome.
14+
15+
Constraints:
16+
17+
1 <= s.length <= 2 * 105
18+
s consists only of printable ASCII characters.
19+
*/
20+
21+
#include <cctype>
22+
#include <string>
23+
#include <vector>
24+
using namespace std;
25+
26+
#pragma region ExtraSpaceIdea
27+
// 새로운 배열을 만들어서 문자열을 정리한 후에 팰린드롬 판별
28+
// 시간 복잡도 : O(n)
29+
// 공간 복잡도 : O(n)
30+
// n은 문자열의 길이일 것이다.
31+
namespace extra_space_idea {
32+
33+
class Solution {
34+
public:
35+
bool isPalindrome(string s) {
36+
vector<char> str;
37+
str.reserve(s.size());
38+
39+
size_t len = s.length();
40+
for (size_t i = 0; i < len; i++) {
41+
if (isalnum(s[i])) {
42+
str.push_back(tolower(s[i]));
43+
}
44+
}
45+
46+
size_t vec_len = str.size();
47+
for (size_t i = 0; i < (vec_len + 1) / 2; i++) {
48+
if (str[i] != str[vec_len - 1 - i]) return false;
49+
}
50+
51+
return true;
52+
}
53+
};
54+
55+
} // namespace extra_space_idea
56+
#pragma endregion
57+
58+
#pragma region FinalSolution
59+
// 투 포인터를 이용해 추가 배열 없이 구현
60+
// 시간 복잡도 : O(n)
61+
// 공간 복잡도 : O(1)
62+
class Solution {
63+
public:
64+
bool isPalindrome(string s) {
65+
int left = 0;
66+
int right = s.length() - 1;
67+
68+
while(left < right) {
69+
if (!isalnum(s[left]))
70+
left++;
71+
else if (!isalnum(s[right]))
72+
right--;
73+
else {
74+
if (tolower(s[left]) != tolower(s[right]))
75+
return false;
76+
left++;
77+
right--;
78+
}
79+
}
80+
81+
return true;
82+
}
83+
};
84+
#pragma endregion

0 commit comments

Comments
 (0)