Skip to content

Commit aa591c5

Browse files
author
kwu130
committed
feat(src): add 7-reverse, 9-isPalindrome, 11-maxArea
1 parent cfc1f00 commit aa591c5

3 files changed

Lines changed: 106 additions & 0 deletions

File tree

‎src/11-maxArea.cpp‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//
2+
// Created by 1412 on 2025/10/8.
3+
//
4+
5+
#include <bits/stdc++.h>
6+
7+
using namespace std;
8+
9+
class Solution {
10+
public:
11+
int maxArea(vector<int>& height) {
12+
int res = 0;
13+
int i = 0, j = height.size() - 1;
14+
while (i < j) {
15+
int area = (j - i) * min(height[i], height[j]);
16+
res = max(res, area);
17+
if (height[i] < height[j]) {
18+
i++;
19+
} else {
20+
j--;
21+
}
22+
}
23+
return res;
24+
}
25+
};
26+
27+
void test() {
28+
Solution s;
29+
vector<int> height = {1, 8, 6, 2, 5, 4, 8, 3, 7};
30+
assert(s.maxArea(height) == 49);
31+
height = {1, 1};
32+
assert(s.maxArea(height) == 1);
33+
}
34+
35+
int main() {
36+
test();
37+
return 0;
38+
}

‎src/7-reverse.cpp‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// Created by 1412 on 2025/9/29.
3+
//
4+
5+
#include <bits/stdc++.h>
6+
7+
using namespace std;
8+
9+
class Solution {
10+
public:
11+
int reverse(int x) {
12+
int res = 0;
13+
while (x) {
14+
if (res < INT_MIN / 10 || res > INT_MAX / 10) {
15+
return 0;
16+
}
17+
res = res * 10 + x % 10;
18+
x /= 10;
19+
}
20+
return res;
21+
}
22+
};
23+
24+
void test() {
25+
Solution s;
26+
assert(s.reverse(123) == 321);
27+
assert(s.reverse(-123) == -321);
28+
assert(s.reverse(120) == 21);
29+
assert(s.reverse(0) == 0);
30+
assert(s.reverse(-2147483412) == -2143847412);
31+
}
32+
33+
int main() {
34+
test();
35+
return 0;
36+
}

‎src/9-isPalindrome.cpp‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//
2+
// Created by 1412 on 2025/10/7.
3+
//
4+
5+
#include <bits/stdc++.h>
6+
7+
using namespace std;
8+
9+
class Solution {
10+
public:
11+
bool isPalindrome(int x) {
12+
if (x < 0 || (x % 10 == 0 && x != 0)) return false;
13+
int revertedNumber = 0;
14+
while (x > revertedNumber) {
15+
revertedNumber = revertedNumber * 10 + x % 10;
16+
x /= 10;
17+
}
18+
return x == revertedNumber || x == revertedNumber / 10;
19+
}
20+
};
21+
22+
void test() {
23+
Solution s;
24+
assert(s.isPalindrome(121) == true);
25+
assert(s.isPalindrome(-121) == false);
26+
assert(s.isPalindrome(10) == false);
27+
}
28+
29+
int main() {
30+
test();
31+
return 0;
32+
}

0 commit comments

Comments
 (0)