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

2024 年 4 月 16 日
 ZZ74

写了超级丑的代码,结果被 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;
        }
1320 次点击
所在节点    分享发现
1 条回复
Sezxy
2024 年 4 月 16 日
这道题同样的代码,18 年的时候是 42ms ,刚才重新提交一次是 1ms

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://v2ex.ih06.com/t/1032948

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX