-
Notifications
You must be signed in to change notification settings - Fork 0
7_ReverseInteger
a920604a edited this page Apr 14, 2023
·
1 revision
title: 7. Reverse Integer
Given a 32-bit signed integer, reverse digits of an integer.
- 要注意是否會overflow,在每次將結果加到ret變數時都必須檢查是否overflow
class Solution {
public:
int reverse(int x) {
int ret = 0;
while(x){
if(abs(ret) > INT_MAX/10) return 0;
ret = 10*ret + (x%10);
x/=10;
}
return ret;
}
};
- time complexity
O(1)
- space complexity
O(1)
footer