• LeetCode8-字符串转换整数(atoi)


    到目前为止比较简单容易理解的一个代码:
    参考链接:
    🔗:【8. 字符串转换整数 String to Integer (atoi) 【LeetCode 力扣官方题解】-哔哩哔哩】

    1.大神解法

    累乘和累加,不需要调用pow函数,比我的垃圾解法快得多!判断整数溢出的点比从后往前算要少!
    判断整数溢出的逻辑是:
    1.如果ans比MAX_value的10分之1大,则乘以10累加之后绝对溢出.
    2.如果我恰好等于max_value的10分之1,则需要计算max_value的个位数,如果在当前ans的最后一位数的前提下,ans的大小恰好等于max_value的 10分之1,并且最后以为大于等于max_value的个位数,则ans乘以10累加max_value个位数之后一腚溢出!这是精准的判断!

    class Solution {
        public int myAtoi(String s) {
            boolean negative=false;
            char[] ch=s.toCharArray();
            int i=0,ans=0,n=ch.length;
            if(s==null||n==0){
                return 0;
            }
            
            while(i<n&&(ch[i]==' ')){
                ++i;
            }
            if(i==n){
                return 0;
            }
            if (ch[i]=='-') {
                negative=true;
            }
            if (ch[i]=='+'||ch[i]=='-') {
                i++;
            }
            while (i < ch.length && ch[i] <= '9' && ch[i] >= '0') {
                int r = ch[i] - '0';
                if (ans>Integer.MAX_VALUE/10||(ans==Integer.MAX_VALUE/10&&r>Integer.MAX_VALUE%10)) {
                    return !negative?Integer.MAX_VALUE:Integer.MIN_VALUE;
                }
                ans=ans*10+r;
                ++i;
            }
            return !negative?ans:-ans;
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    2.我的辣鸡解法:

    公式是:
    ans = ans + digit*Math.pow
    从后往前算,累加,比较费劲,不如大神解法来的直接!大神解法维护的变量少,而且速度更快!而且我的这个算法有两个整数溢出的点,需要判断整数溢出的语句更多更复杂!

    class Solution {
        public int myAtoi(String s) {
            if(s==null||s.length()==0){
                return 0;
            }
            boolean hashNum=false,negative=false;
            char[] ch=s.toCharArray();
            int i=0,ans=0,n=ch.length;
            while(i<n&&(ch[i]==' ')){
                ++i;
            }
    
            int tmp_idx=i;
            while(i<n&&!(ch[i]>='1'&&ch[i]<='9')&&(ch[i]!='-'&&ch[i]!='+')){
                ++i;
            }
           
            if(i<n&&(ch[i]=='-'||ch[i]=='+')){
                negative= ch[i]=='+'?false:true;
                i=tmp_idx+1;
            }
            else{//没有+的情况
                negative=false;
                i=tmp_idx;
            }
            while(i<n&&ch[i]=='0'){
                ++i;
            }
            while(i<n){
    
                if((ch[i]<='9'&&ch[i]>='1')){
                    break;
                }else{
                    return 0;
                }
            }
            int j=i;
    
            while(j<n){
                if((ch[j]<='9'&&ch[j]>='0')){
                    ++j;
                }
                else{
                    --j;
                    break;
                }
                
            }
            if(j>=n||(!(ch[j]<='9'&&ch[j]>='0'))){
                --j;
            }
            int itg=j-i;//假如是3位数,则itg=2,10位数,itg=9
            if(itg>10){
                return !negative?Integer.MAX_VALUE:Integer.MIN_VALUE;
            }
            int index=0;//10^(index)次方
            while(index<=itg){
                if(((ch[j]-'0')>=3&&index>=9)||((ch[j]-'0')>=2&&index>=10)){
                    return !negative?Integer.MAX_VALUE:Integer.MIN_VALUE;
                }
                int rhs=((ch[j]-'0')*(int)Math.pow(10,index));
                if(ans>Integer.MAX_VALUE-rhs){
                    return !negative?Integer.MAX_VALUE:Integer.MIN_VALUE;
                }else {
                    ans+=rhs;
                }
                ++index;
                --j;
                if(j-i>=itg){
                    break;
                }
            }
            return !negative?ans:-ans;
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76

    3.整数相加的溢出判断(chaGPT代码)

    public class OverflowExample {
        public static void main(String[] args) {
            int a = 2147483647;  // 最大的int值
            int b = 1;
    
            if (willAdditionOverflow(a, b)) {
                System.out.println("Overflow detected!");
            } else {
                int result = a + b;
                System.out.println("Result: " + result);
            }
        }
    
        private static boolean willAdditionOverflow(int a, int b) {
            // 如果 a 和 b 同号,且它们的和与 a 的符号不同,即溢出
            if (b > 0 && a > Integer.MAX_VALUE - b) {
                return true;
            }
            if (b < 0 && a < Integer.MIN_VALUE - b) {
                return true;
            }
            return false;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    4.整数相乘溢出判断(chatGPT代码)

    对于整数相乘,溢出的判断逻辑相对复杂一些,因为整数相乘可能会导致更大范围的结果。以下是一个通用的整数相乘的溢出判断逻辑的代码:

    public class OverflowExample {
        public static void main(String[] args) {
            int a = 2147483647;  // 最大的int值
            int b = 2;
    
            if (willMultiplicationOverflow(a, b)) {
                System.out.println("Overflow detected!");
            } else {
                int result = a * b;
                System.out.println("Result: " + result);
            }
        }
    
        private static boolean willMultiplicationOverflow(int a, int b) {
            // 特殊处理乘法溢出的边界情况
            if (a == 0 || b == 0) {
                return false;
            }
    
            // 如果 a 为正,且 b 大于 Integer.MAX_VALUE / a,或者 a 为负,且 b 小于 Integer.MIN_VALUE / a,即溢出
            if ((a > 0 && b > Integer.MAX_VALUE / a) || (a < 0 && b < Integer.MIN_VALUE / a)) {
                return true;
            }
    
            return false;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    在这个代码中,willMultiplicationOverflow 方法首先处理了特殊情况,即其中一个因子为0。然后,它检查是否会发生溢出,主要是通过判断 a 的正负以及 b 是否足够大或足够小。这是因为 Integer.MAX_VALUE 和 Integer.MIN_VALUE 在绝对值上是相差1的,而在相乘时,结果的范围是 [Integer.MIN_VALUE, Integer.MAX_VALUE],因此需要额外的条件判断。

    请注意,在实际编程中,可能需要更复杂的判断逻辑,尤其是在处理更大范围的整数时。此外,如果需要处理更大的整数,可能需要考虑使用 long 类型,因为它的范围更广。

  • 相关阅读:
    CMake
    kalman滤波与目标跟踪2: kalman滤波代码讲解
    【毕业设计】深度学习垃圾分类系统 - python 卷积神经网络
    阿里 P7 到底是怎样的水平?
    OpenCL专题04:ViennaCL与Eigen双剑合璧
    配置JVM堆栈大小
    多版本node的安装与切换详细操作
    vue对于低版本浏览器兼容问题
    查看进程占用端口及杀死进程命令
    【NLP】第6章 使用 Transformer 进行机器翻译
  • 原文地址:https://blog.csdn.net/weixin_46028606/article/details/134542960