-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp7.cpp
More file actions
58 lines (44 loc) · 1.15 KB
/
p7.cpp
File metadata and controls
58 lines (44 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* Easy
* https://leetcode.com/problems/reverse-integer/
*/
#include <iostream>
#include <string>
#include <vector>
using std::cout;
using std::endl;
class Solution {
public:
int reverse(int x) {
// std::string text = "123";
// int n = std::stoi(text);
std::vector<char> v;
std::string str = std::to_string(x);
int lower;
if (x < 0) {
v.push_back('-');
lower = 1;
}
else lower = 0;
for (int i = str.length()-1; i >= lower; i--)
v.push_back(str.at(i));
if ((lower == 1 && str.length() > 11)
|| (lower == 0 && str.length() > 10))
return 0;
std::string ret_s(v.begin(), v.end());
long ret_l = std::stol(ret_s);
if (ret_l < -2147483648 || ret_l > 2147483647)
return 0;
return ret_l;
}
};
int main(void) {
Solution sol = Solution();
int arg1 = -122;
int arg2 = 241;
cout << "arg1: " << arg1 << endl;
cout << "output: " << sol.reverse(arg1) << endl;
cout << "arg2: " << arg2 << endl;
cout << "output: " << sol.reverse(arg2) << endl;
return 0;
}