Solution without LongLong:
#include <string>
class Solution {
public:
int reverse(int x) {
if (x == 0) return 0;
else {
bool type = x < 0 ? false : true;
std::string s = to_string(x);
std::string r, maximum, minimum;
maximum = "2147483647";
minimum = "2147483648";
int point = s.size();
while (s[--point] == '0') {}
for (int i = point; i >= 0; i--) r.push_back(s[i]);
point = s.size();
if (0 < x) {
if (x < 1000000000) return std::stoi(r);
else {
for (int i = 0; i < point; i++) {
if (maximum[i] < r[i]) return 0;
else if (maximum[i] == r[i]) continue;
else break;
}
return std::stoi(r);
}
}
else {
if (-1000000000 < x) return -std::stoi(r);
else {
for (int i = 0; i < point; i++) {
if (minimum[i] < r[i]) return 0;
else if (minimum[i] == r[i]) continue;
else break;
}
return -std::stoi(r);
}
}
}
}
};
|
cs |
'Leetcode' 카테고리의 다른 글
Merge Two Sorted Lists / leetcode / c++ (0) | 2022.06.01 |
---|---|
Remove Nth Node From End of List, LeetCode, c++/cpp/c (0) | 2022.06.01 |
Add Two Numbers leetcode 2 (0) | 2022.05.29 |
[MySQL] 176. Second Highest Salary[LeetCode Medium] - 1 (0) | 2022.05.25 |
[MySQL] 175. Combine Two Tables [LeetCode Easy] (0) | 2022.05.25 |