ReverseInteger:实现int整数的反转

原文链接

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
Note:
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [?231, 231 ? 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

class Solution1 {
    public int reverse(int x) {
        while (x % 10 == 0) {
            x /= 10;
        }
        boolean tag = false;
        if (x < 0) {
            tag = true;
            x = x * -1;
        }
        String s = String.valueOf(x);
        char[] ch = s.toCharArray();
        for (int i = 0;i < ch.length;i++) {
            int k = ch.length - i - 1;
            if (k < ch.length / 2) break;
            char cc = ch[i];
            ch[i] = ch[k];
            ch[k] = cc;
        }
        String re = String.valueOf(ch);
        int RI = Integer.valueOf(re);
        if (tag) RI = RI * -1;
        System.out.println(RI);
        return RI;
    }
}
/*
* 1. 前几天刚做了字符反转的,想当然就以为可以直接拿来用。结果却和出题人意图相差甚远.Solution1的方案对于小部分的测试数据可行,但是当前的状态是行不通的,也没有继续对此进行修改完善.
* 2. 一开始做的时候下面的Solution思路类似,但是因为对算法的逻辑思路不是很透彻,写出来的代码肯定很杂乱繁琐。下面的这个解题思路有参考到Discussion中的一个答案.
* 3. 昨晚做出来之后一直不晓得哪里出错了,没有意识到int类型的边界范围,还特别纳闷为啥断点跟踪的时候1534236469这个数字最后一下子就变了.根本没有考虑过怎么处理"int类型数据溢出"问题
*   上网搜索了一下之后才晓得怎么判断int类型问题。https://www.cnblogs.com/hesiyi/p/6963435.html
*
* */
class Solution {
    public int reverse(int x) {
        int sum = 0;
        int t = 0;
        while (x != 0) {
            if ((sum * 10L) > Integer.MAX_VALUE || (sum * 10L) < Integer.MIN_VALUE)
                return 0;
            sum = sum * 10 + x % 10;
            x = x / 10;
        }
        return sum;
    }
}
public class ReverseInteger {
    public static void main(String[] args) {
        Solution1 solu = new Solution1();
        int kkk = solu.reverse(-214748364); // -2147483648,1534236469
        System.out.println(kkk);
    }
}

关于int类型数据溢出的问题,移步博客:java-int数据的溢出

原文地址:https://www.cnblogs.com/leonwen/p/10486730.html

时间: 2024-07-31 13:50:41

ReverseInteger:实现int整数的反转的相关文章

int整数和bool值

# ########################################## int 整数 ########################################## # 1. 当前整数的二进制表示,最少位数 # age = 4 # 100 # print(age.bit_length()) # 2. 获取当前数据的字节表示 # age = 15 # v = age.to_bytes(10,byteorder='big') # v = age.to_bytes(10,byt

7、str字符串、int整数、list列表、dict字典、set集合、tuple元祖功能详解

1 ######################################## str 字符串 及类中的各种技能########################################## 2 3 # 1. capitalize 字符串首字母大写 4 # 自身不变,会生成一个新的值 5 # name = 'deasion' # str类的对象 6 # v = name.capitalize() # 自动找到name关联的str类,执行其中的capitalize技能 7 # prin

将一个int数组变为一个int整数

一个面试题: java 实现将一个int数组变为一个int整数.例如:int[] arr = new int[]{1,2,3,4,5}; 变为12345 /**  * Created by leo on 15/10/27.  */ public class Interview {     public static void main(String[] args) {         System.out.println(parseInt(new int[]{1,2,3,4,5}));     

Python学习之[int 整数][bool]

1.int 整数需要记住的一个操作 bit_length() 计算一数的二进制长度 2.bool(类型转换的问题) 没有操作 类型转换 字符串转换成整数 int(str) 结论1: 想要转化成什么类型就用这个类型括起来结论2: True => 1 False => 0结论3: 可以当做False来用的数据: 0 "" [] {} () None   原文地址:https://www.cnblogs.com/charles-lin/p/9594960.html

c程序十六进制字符串转换为整数与反转

字符串转整数使用sscanf int value = 0; char *buf = "1d5ce"; sscanf (buf, "%x", &value); printf ("Hex value is:%x\n", value); 整数转字符串使用sprintf int num = 0; char buf[10] = {}; num = 120270; sprintf (buf, "%x", num); //打印 bu

两个int整数m和n的二进制表达有多少个位不同

题目描述 世界上有10种人,一种懂二进制,一种不懂.那么你知道两个int32整数m和n的二进制表达,有多少个位(bit)不同么? 输入例子: 1999 2299 输出例子: 7 题目分析 二进制中,统计两个数的相应位(bit)相同可以采用异或操作,异或运算结果相同为0不同为1.所以两个数异或的结果中1就是相同位数. public static int count(int a, int b) { int m = a ^ b; int num = 0; while(m>0) { m &= (m-

C语言atoi()函数:将字符串转换成int(整数)

头文件:#include <stdlib.h> atoi() 函数用来将字符串转换成整数(int),其原型为:int atoi (const char * str); [函数说明]atoi() 函数会扫描参数 str 字符串,跳过前面的空白字符(例如空格,tab缩进等,可以通过 isspace() 函数来检测),直到遇上数字或正负符号才开始做转换,而再遇到非数字或字符串结束时('\0')才结束转换,并将结果返回. [返回值]返回转换后的整型数:如果 str 不能转换成 int 或者 str 为

int(整数)功能详解

class int(object):    """    int(x=0) -> int or long    int(x, base=10) -> int or long        Convert a number or string to an integer, or return 0 if no arguments    are given.  If x is floating point, the conversion truncates toward

[c++]关于倒转32位int整数的范围控制问题

题目来源:leetcode Reverse Integer Given a 32-bit signed integer, reverse digits of an integer. Example 1:            Example 2:          Example 3: Input: 123             Input: -123         Input: 120 Output: 321          Output: -321       Output: 21 N