7. Reverse Integer
Given a 32-bit signed integer, reverse digits of an integer.
Example 1:
Input: 123 Output: 321
Example 2:
Input: -123 Output: -321
Example 3:
Input: 120 Output: 21
Note:
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [โ231, 231 โ 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
์ฝ๋
#include <iostream>
using namespace std;
class Solution {
public:
int reverse(int x) {
long long int answer = 0;
while (x != 0) {
// INT_MAX๊ฐ overflow, underflow๊ฐ ์ผ์ด๋๋ ์กฐ๊ฑด์ ๋ง์ถฐ 0์ ๋ฐํํ๋ค.
if (((answer * 10) > INT_MAX) || ((answer * 10) < INT_MIN))
return 0;
answer *= 10;
answer += x % 10;
x /= 10;
}
return answer;
}
};
int main() {
int n;
cout << "-2,147,483,646 ~ 2,147,483,647 ์ฌ์ด์ ๊ฐ์ ์
๋ ฅํ์ธ์ :";
cin >> n;
Solution s;
cout << s.reverse(n);
return 0;
}
Success
Runtime: 0 ms, faster than 100.00% of C++ online submissions for Reverse Integer.
Memory Usage: 5.9 MB, less than 100.00% of C++ online submissions for Reverse Integer.
'๐ฅ๏ธ Computer > Algorithm Solution' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[LeetCode] 397. Integer Replacement (0) | 2020.03.24 |
---|---|
[LeetCode] 278. First Bad Version (0) | 2020.03.24 |
[BAEKJOON] 7568๋ฒ : ๋ฉ์น (0) | 2020.03.20 |
[BAEKJOON] 1152๋ฒ : ๋จ์ด์ ๊ฐ์ (0) | 2020.03.19 |
[BAEKJOON] 2444๋ฒ : ๋ณ ์ฐ๊ธฐ-7 (0) | 2020.03.18 |
7. Reverse Integer
Given a 32-bit signed integer, reverse digits of an integer.
Example 1:
Input: 123 Output: 321
Example 2:
Input: -123 Output: -321
Example 3:
Input: 120 Output: 21
Note:
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [โ231, 231 โ 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
์ฝ๋
#include <iostream>
using namespace std;
class Solution {
public:
int reverse(int x) {
long long int answer = 0;
while (x != 0) {
// INT_MAX๊ฐ overflow, underflow๊ฐ ์ผ์ด๋๋ ์กฐ๊ฑด์ ๋ง์ถฐ 0์ ๋ฐํํ๋ค.
if (((answer * 10) > INT_MAX) || ((answer * 10) < INT_MIN))
return 0;
answer *= 10;
answer += x % 10;
x /= 10;
}
return answer;
}
};
int main() {
int n;
cout << "-2,147,483,646 ~ 2,147,483,647 ์ฌ์ด์ ๊ฐ์ ์
๋ ฅํ์ธ์ :";
cin >> n;
Solution s;
cout << s.reverse(n);
return 0;
}
Success
Runtime: 0 ms, faster than 100.00% of C++ online submissions for Reverse Integer.
Memory Usage: 5.9 MB, less than 100.00% of C++ online submissions for Reverse Integer.
'๐ฅ๏ธ Computer > Algorithm Solution' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[LeetCode] 397. Integer Replacement (0) | 2020.03.24 |
---|---|
[LeetCode] 278. First Bad Version (0) | 2020.03.24 |
[BAEKJOON] 7568๋ฒ : ๋ฉ์น (0) | 2020.03.20 |
[BAEKJOON] 1152๋ฒ : ๋จ์ด์ ๊ฐ์ (0) | 2020.03.19 |
[BAEKJOON] 2444๋ฒ : ๋ณ ์ฐ๊ธฐ-7 (0) | 2020.03.18 |