Skip to content

Commit fe1cfaa

Browse files
committed
3주차 문제 풀이 2개 추가
- Decode Ways - Maximum Subarray
1 parent d4a3f56 commit fe1cfaa

2 files changed

Lines changed: 70 additions & 0 deletions

File tree

decode-ways/hwi-middle.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution {
2+
public:
3+
int numDecodings(string s) {
4+
int d[101];
5+
int len = s.size();
6+
7+
if (s[0] == '0') // 0으로 시작할 수 없음
8+
{
9+
return 0;
10+
}
11+
12+
d[0] = 1; // 빈 문자열 -> 1가지 방법
13+
d[1] = 1; // 첫 문자 -> 1가지 방법 (0이 아닌건 확인했음)
14+
15+
for (int i = 2; i <= len; ++i)
16+
{
17+
d[i] = 0;
18+
19+
// 0이 아니어서 단독으로 해석 가능한 경우
20+
if (s[i - 1] != '0')
21+
{
22+
d[i] += d[i - 1];
23+
}
24+
25+
// 앞글자와 조합해서 유효한 두 자리가 될 수 있는 경우
26+
if (s[i - 2] == '1' || (s[i - 2] == '2' && s[i - 1] <= '6'))
27+
{
28+
d[i] += d[i - 2];
29+
}
30+
}
31+
32+
return d[len];
33+
}
34+
};

valid-palindrome/hwi-middle.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution {
2+
public:
3+
bool isPalindrome(string s) {
4+
int len = s.size();
5+
int l = 0;
6+
int r = len - 1;
7+
8+
while (l <= r)
9+
{
10+
while (l <= r &&!isalnum(s[l]))
11+
{
12+
l++;
13+
}
14+
15+
while (l <= r && !isalnum(s[r]))
16+
{
17+
r--;
18+
}
19+
20+
if (l > r)
21+
{
22+
break;
23+
}
24+
25+
if (tolower(s[l]) != tolower(s[r]))
26+
{
27+
return false;
28+
}
29+
30+
l++;
31+
r--;
32+
}
33+
34+
return true;
35+
}
36+
};

0 commit comments

Comments
 (0)