算法练习day1

   Day1:今日练习两题。

题目:整数反转 难度:简单

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

注意:

假设我们的环境只能存储得下 32 位的有符号整数,则其数值范围为 [−231,  231 − 1]。请根据这个假设,如果反转后整数溢出那么就返回 0

自己的思路1:

给一个数组存储每次x取模后说得的值然后再进行输出,但是只能计算正数,所以不可取,而且运行时输出有问题但是感觉逻辑上没问题,等等开idea debug看看。附上代码:

 

自己的思路2

采用StringBuffer的字符串反转(StringBuffer.reverse方法),先将x转换成为字符串,若为负数最后在输出前添加-就行,写一个try catch语句就不需要判断是否溢出的情况。附上代码:

 

题目思路:

可以一次构建反转整数的一位数字。在这样做的时候,我们可以预先检查向原整数附加另一位数字是否会导致溢出。

附上代码

class Solution {

    public int reverse(int x) {

        int rev = 0;

        while (x != 0) {

            int pop = x % 10;

            x /= 10;

            if (rev > Integer.MAX_VALUE/10 || (rev == Integer.MAX_VALUE / 10 && pop > 7)) //判断是否会溢出

 return 0;

            if (rev < Integer.MIN_VALUE/10 || (rev == Integer.MIN_VALUE / 10 && pop < -8)) return 0;

            rev = rev * 10 + pop;

        }

        return rev;

    }

}

题目:字符串转换整数(atoi) 难度:中等

https://leetcode-cn.com/problems/string-to-integer-atoi/comments/

 

自己思路:

首先将str字符串转为char数组

将前面的空白字符去除

判断第一个非空字符是否是符号位,若不是符号位也不是有效的整数字符时return 0

随后判断字符大小是否为0~9之间并可用上一题中的x=10*x+integer.valueOf(char[i)]存储,当遇到不是不是数字时循环结束。并且判断该x大小是否溢出,若不溢出就return该值。

代码:

public int myAtoi(String str) {
    int x = 0; //存储的值
    if (str.isEmpty())
        return 0;
    else{
        int i = 0; //去除开始的空字符
        while(i<str.length() && str.charAt(i) == ' ')
            i++;
        // 判断第一个非空字符是否是符号位,
        boolean fh = true;//记录符号位 true+ false-
        if(i<str.length() && (str.charAt(i) == '-' || str.charAt(i) == '+')){
            //判断只有一个符号位
            if(str.length() == 1)
                return 0;
            if(str.charAt(i) == '-'){
                fh = false;
                i++;
                //若不是0~9的整数字符就return 0
                if(str.charAt(i) > '9' || str.charAt(i) < '0'){
                    return 0;
                }
            }else{
     i++;
     //若不是0~9的整数字符就return 0
     if(str.charAt(i) > '9' || str.charAt(i) < '0'){
         return 0;
     }
}
        }
        while(i<str.length() && (str.charAt(i) < '0' || str.charAt(i) > '9'))
            return 0;
        //这里要小心数组越界 - - 报错了
        System.out.println("````````");
        while (i<str.length() && str.charAt(i) >= '0' && str.charAt(i) <= '9'){
            System.out.println(str.charAt(i));
            //使用try来应对溢出
           /* try {
                //x = 10*x+ Integer.valueOf(str.charAt(i));
                x = 10*x + (str.charAt(i) - '0');
                i++;
            }catch (Exception e){
                return 0;
            }*/
            if (x > Integer.MAX_VALUE / 10 || (x == Integer.MAX_VALUE / 10 && str.charAt(i) - '0' > 7))
            { return (fh) ? Integer.MAX_VALUE : Integer.MIN_VALUE; }
            x = 10*x + (str.charAt(i) - '0');
            i++;
        }
        if(fh)
            return x;
        else
            return -x;
    }
}

一定要小心数组越界,还要知道与或的运算顺序,很容易数组越界,提交代码后总会有一些可能没有覆盖到或者输出错误,然后不能用trycatch处理数组越界,这里的判断是网上copy下来的。用try会有这个错误。

 

太特么惨了- -

运行时间还可以优化,先优化下代码,将一些冗余的东西删了然后整合了下。

class Solution {
    public int myAtoi(String str) {
        int x = 0; //存储的值
        if (str.isEmpty())
            return 0;
        else{
            int i = 0; //去除开始的空字符
            while(i<str.length() && str.charAt(i) == ' ')
                i++;
            // 判断第一个非空字符是否是符号位,
            boolean fh = true;//记录符号位 true+ false-
            if(i<str.length() && (str.charAt(i) == '-' || str.charAt(i) == '+')){
                if(str.charAt(i) == '-')
                    fh = false;
                    i++;
            }
            while(i<str.length() && (str.charAt(i) < '0' || str.charAt(i) > '9'))
                return 0;
            while (i<str.length() && str.charAt(i) >= '0' && str.charAt(i) <= '9'){
                if (x > Integer.MAX_VALUE / 10 || (x == Integer.MAX_VALUE / 10 && str.charAt(i) - '0' > 7))
                { return (fh) ? Integer.MAX_VALUE : Integer.MIN_VALUE; }
                x = 10*x + (str.charAt(i) - '0');
                i++;
            }
            if(fh)
                return x;
            else
                return -x;
        }
    }
}

优化后的运行时间:

 

总结:-- 。。。睡觉,狗命重要。

猜你喜欢

转载自www.cnblogs.com/xyu777/p/10212111.html