zoukankan      html  css  js  c++  java
  • leetcode算法-整数反转

    题目描述:
    给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。

    我的解答:
    package Simple;
    import java.util.Scanner;
    public class Practice2 {
        private int i = 1;
        // 默认为整数,即无符号
        private String sign = "";
    
        public static void main(String[] args) {
            Practice2 sol = new Practice2();
            sol.getNum();
        }
    
        private void getNum() {
            System.out.println("请输入要反转的整数:");
            //判断输入的整数是几位数
            Scanner input = new Scanner(System.in);
            //n为输入的整数
            int n = input.nextInt();
    //        System.out.println(n);
            input.close();
            //首先判断是否越界
    //        if (n < Integer.MIN_VALUE || n > Integer.MAX_VALUE)
    //            System.out.println("输入的整数越界!");
            // 接着判断符号
            // 如果是负数,则先把符号去掉,把绝对值反转之后再加上负号
            if (n < 0)
                this.sign = "-";
            n = Math.abs(n);
            //求整数为几位数
            int m = n;
            while (m > 9) {
    //            // m除以10表示位数多了一位
                m = m / 10;
                this.i++;
    //            //如果m/10仍然>9,则继续进入while循环
            }
            if (n < 10)
                System.out.println("输入的整数为一位数,反转后的数为:" + this.sign + n);
            else {
                System.out.println("输入的整数为" + i + "位数");
                //首先要注意是有符号整数,负数反转之后仍然为负数
                //其次要注意范围是32位,如果溢出就返回0
                //反转后的整数
                int res = this.reverse(n);
                System.out.println("反转后的整数为:" + this.sign + res);
            }
    
        }
    
        private int reverse(int x) {
            int p, w, t, s, m = x, sum = 0;
            //这里我们对反转方法只考虑正整数,且n>10
            //有几位数就输出几次
            for (int k = this.i; k >= 1; k--) {
                t = 1;
                s = 1;
                //第k位数就除以10的k-1次方
                for (int j = 1; j <= k - 1; j++) {
                    t *= 10;
                }
                //得到第k位数的值
                m = m / t;
                w = this.i - k;
                // l=Math.pow(10,w);
                for (int f = 1; f <= w; f++) {
                    s *= 10;
                }
                p = m * s;
                sum += p;
                //将首位去掉,对剩下的数重复输出
                m = x % t;
            }
            return sum;
        }
    }

    自我分析:

    自己写得很复杂,过程比较麻烦,在别人的评论中看到一个不错的回答,学习经验:
    更优解答:
    package Simple;
    
    import java.util.Scanner;
    
    public class Test {
        public static void main(String[] args) {
            Test t=new Test();
            Scanner input = new Scanner(System.in);
            System.out.println("请输入要反转的整数:");
            //n为输入的整数
            int n = input.nextInt();
            input.close();
            int res=t.reverse(n);
            System.out.println(res);
        }
        public int reverse(int x) {
            long rs = 0;
            while(x != 0){
                rs = rs*10+x%10;
                x /= 10;
            }
            return (rs<Integer.MIN_VALUE || rs>Integer.MAX_VALUE) ? 0:(int)rs;
        }
    }




  • 相关阅读:
    oracle插入数据
    保存图片
    ASCII码排序及md5加密
    JavaScript
    HTML
    py访问Redis和zk操作
    Zookeeper集群搭建以及python操作zk
    并发编程
    Python之socket(套接字)
    Python 网络编程
  • 原文地址:https://www.cnblogs.com/iceywu/p/11773241.html
Copyright © 2011-2022 走看看