leetcode——Reverse Integer 反转整数数字(AC)

Reverse digits of an integer.

Example1: x = 123, return 321

Example2: x = -123, return -321

这个题比较简单,考虑特殊情况如12000,注意检查反转后数字是否会越界溢出。代码如下:

class Solution {
public:
    int reverse(int x) {
         bool minus = false;
		short int splitNum[10];
		int i = 0, j = 0;
		unsigned int num, result = 0;
		unsigned int MAX = 2147483647;
		unsigned int MIN = 2147483648;
        if(x < 0)
		{
			num = 0 - x;
			minus = true;
		}
		else
		{
			num = x;
		}
		while(num != 0)
		{
			splitNum[i++] = num%10;
			num = num/10;
		}
		while(splitNum[j] == 0)
			j++;
		while(j < i)
		{
			if(!minus && (MAX-splitNum[j])/10 >= result)
			{
				result = result*10 + splitNum[j];
				j++;
			}
			else if(minus && (MIN-splitNum[j])/10 >= result)
			{
				result = result*10 + splitNum[j];
				j++;
			}
			else
			{
				return 0;
			}
		}
		return minus ? (0-result) : result;
    }
};

leetcode——Reverse Integer 反转整数数字(AC)

时间: 2024-08-06 11:57:48

leetcode——Reverse Integer 反转整数数字(AC)的相关文章

LeetCode Reverse Integer 反转整数

1 class Solution { 2 public: 3 int reverse(int x) { 4 int e,s,num,k=0; 5 num=x; 6 e=0; 7 if(x<0) 8 num=-1*x; 9 while( num!=0 ){ 10 s=num%10; 11 e=e*10+s; 12 num=num/10; 13 k++; 14 } 15 if(x<0) 16 return -e; 17 else 18 return e; 19 } 20 }; 题意: Exampl

[leetcode]7. Reverse Integer反转整数

Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Example 2: Input: -123 Output: -321 Example 3: Input: 120 Output: 21 题意: 给定一个10进制整数,翻转它. Solution1: directly do the simulation. Two tricky parts to be hand

[LeetCode] Reverse Integer 翻转整数

Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought throu

7. Reverse Integer 反转整数

[抄题]: 将一个整数中的数字进行颠倒,当颠倒后的整数溢出时,返回 0 (标记为 32 位整数). 样例 给定 x = 123,返回 321 给定 x = -123,返回 -321 [暴力解法]: 时间分析: 空间分析: [优化后]: 时间分析: 空间分析: [奇葩输出条件]: [奇葩corner case]: 负数.末尾有0均可用该方法处理 [思维问题]: [一句话思路]: 分离一位数.一边变长 另一边变短 [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理

[LeetCode] Reverse Integer [8]

题目 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought th

LeetCode——Reverse Integer

Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought through this! If the integer'

7. Reverse Integer 反转int

7. Reverse Integer 问题: 反转int,当有负号时需要保留负号. 解决思路: 1.先将int类型转换为string,按照之前写过的string类型做好反转,再转为int类型. 2.不做类型转换.先将负数转换为正数进行统一处理,然后int类型数每次%10得到的余即依次为个.十.百-位上的数字. 自己写的1:(java) 这里是根据思路1来做的,发现在string类型转为int类型时容易抛出异常,无法转换.因此只能采用其他方法. public class Solution { pu

Reverse Integer - 反转一个int,溢出时返回0

Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 若反转的数溢出,直接返回0 可以用计算结果来判断溢出,也可以用因数来判断 Java代码实现: 1 public class ReverseInteger { 2 public static int reverseInt(int x){ 3 if (x == 0) { 4 return

[LintCode] Reverse Integer 翻转整数

Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-bit integer). Have you met this question in a real interview? Example Given x = 123, return 321 Given x = -123, return -321 LeetCode上的原题,请参见我之前的博客Reverse Integer.