adding lecture 24

This commit is contained in:
Jidong Xiao
2023-11-20 16:34:28 -05:00
parent 9db1fa8d29
commit fe228831b3
2 changed files with 277 additions and 0 deletions

View File

@@ -0,0 +1,24 @@
// leetcode problem 7: reverse integer
class Solution {
public:
int reverse(int x) {
int sign=1;
if(x<0){
sign = -1;
}
long result = 0;
x = abs(x);
// convert x to a string and reverse it
string s = to_string(x);
std::reverse(s.begin(),s.end());
try{
result = stoi(s);
}catch(const std::out_of_range& e){
cout<<"Error: "<<e.what()<<": out of range"<<endl;
return 0;
}
result = sign * result;
return result;
}
};