V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
ZZ74
V2EX  ›  分享发现

8. 字符串转换整数 (atoi) 超级丑的代码,反复调试竟然过了

  •  
  •   ZZ74 · 13 天前 · 342 次点击

    写了超级丑的代码,结果被 leetcode 的结果震惊了。 目前还没去看题解...不知道标准答案多简洁
    1ms
    击败 100.00%使用 Java 的用户

      public int myAtoi(String s) {
                int ng=1;
                int cur=0;
                int condition=0;
                int i=0;
                char chr = '\0';
                while (i<s.length()) {
                   if(condition==0){  //start
                       chr=s.charAt(i++);
                       condition++;
                   }else if(condition==1){  //remove blank
                       while (chr==' ' && i<s.length()){
                           chr=s.charAt(i++);
                       }
                       condition++;
                   } else if(condition==2){ //check '-,+'
                       if(chr=='-'){
                           ng=-1;
                           chr=s.charAt(i++);
                           condition++;
                           continue;
                       }
                       if(chr=='+'){
                           chr=s.charAt(i++);
                           condition++;
                           continue;
                       }
                       if((chr>='0' && chr<='9')){
                           condition+=2;
                           continue;
                       }
                       return cur;
                   }else if(condition==3){ //remove '0'
                       while (chr=='0'){
                           chr=s.charAt(i++);
                       }
                       condition++;
                   }
                   else if(condition==4){
                       if(chr<'0'||chr>'9'){
                           return cur;
                       }
                       int value = (chr-'0')*ng;
                       if(cur>Integer.MAX_VALUE/10){
                           return Integer.MAX_VALUE;
                       } else if (cur<Integer.MIN_VALUE/10) {
                           return Integer.MIN_VALUE;
                       } else if (cur==Integer.MAX_VALUE/10) {
                           if (Integer.MAX_VALUE%10<value){
                               return Integer.MAX_VALUE;
                           }
                       }else if (cur==Integer.MIN_VALUE/10) {
                           if (Integer.MIN_VALUE%10>value){
                               return Integer.MIN_VALUE;
                           }
                       }
                       cur = cur*10+value;
                       chr=s.charAt(i++);
                   }
                }
                if(i==s.length()&&chr>='0'&&chr<='9'){
                    int value = (chr-'0')*ng;
                    if(cur>Integer.MAX_VALUE/10){
                        return Integer.MAX_VALUE;
                    } else if (cur<Integer.MIN_VALUE/10) {
                        return Integer.MIN_VALUE;
                    } else if (cur==Integer.MAX_VALUE/10) {
                        if (Integer.MAX_VALUE%10<value){
                            return Integer.MAX_VALUE;
                        }
                    }else if (cur==Integer.MIN_VALUE/10) {
                        if (Integer.MIN_VALUE%10>value){
                            return Integer.MIN_VALUE;
                        }
                    }
    
                    cur = cur*10+value;
    
                }
    
                return cur;
            }
    
    1 条回复    2024-04-16 15:10:15 +08:00
    Goooooos
        1
    Goooooos  
       13 天前
    这道题同样的代码,18 年的时候是 42ms ,刚才重新提交一次是 1ms
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1096 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 18:40 · PVG 02:40 · LAX 11:40 · JFK 14:40
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.