Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions C++/reverse-integer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,22 @@ class Solution {
return result;
}
};


//Time : O(1)
class Solution2 {
public:
int reverse(int x) {
long long ans = 0;

if(x/10 == 0)
return x;

while(x){
ans = ans*10 + x%10;
x = x/10;
}

return (ans > INT_MAX || ans < INT_MIN) ? 0 : ans;
}
};