本文主要是介绍leetcode No7. Reverse Integer,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Question:
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
Algorithm:
一位一位的取,然后乘以10
while循环的条件一定要写>0,因为如果-2147483648 程序就陷入死循环
Submitted Code:
class Solution {
public:int reverse(int x) {long long int temp = x>0?x:-x;long long int num = 0;while(temp>0){num = num*10 + temp%10;temp/=10;}if(num>INT_MAX)return 0;return x>0?num:-num;}
};
Accepted Solutions Runtime Distribution:
这篇关于leetcode No7. Reverse Integer的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!