leetcode刷题40

今天刷的题是LeetCode第8题,字符串转整数

这个题比较麻烦,我自己的代码有些考虑不全,如下:

public class MyAtoi_8_middle {public static int solution(String str){
        String maxNum=String.valueOf(Integer.MAX_VALUE);
        String minNum=String.valueOf(Integer.MIN_VALUE);
        if (str.length()==0)return 0;
        int n=str.length();
        int i=0;
        boolean flag=true;//该数据是正数
        StringBuilder result=new StringBuilder();
        boolean observation=false;//是数字或是负号的时都是true,其他时候转为false
        while (i<n&&str.charAt(i)!='-'&&(str.charAt(i)<'0'||str.charAt(i)>'9')){
            i++;
        }
        str=str.substring(i,n);
        n=str.length();
        i=0;
        if (str.length()==0||(str.length()==1&&str.charAt(0)=='-'))return 0;
        if (str.charAt(i)=='-'){
            if (str.charAt(1)>='0'&&str.charAt(1)<='9'){
                //是一个负数
                i++;
                flag=false;
            }else {
                   
            }
        }
        while (i<n){
            if (str.charAt(i)>='0'&&str.charAt(i)<='9'){
                result.append(str.charAt(i));
                i++;
            }else {
                break;
            }
        }
        if (!flag){
            result.insert(0,'-');
        }
        if (flag){
            //是一个正数
            if (result.length()<maxNum.length()){
                return Integer.parseInt(result.toString());
            }else if (result.length()>maxNum.length()){
                return Integer.MAX_VALUE;
            }else {
                if (compare(result.toString(),maxNum,true)){
                    return    Integer.parseInt(result.toString());
                }else {
                    return Integer.MAX_VALUE;
                }
            }
        }else {
            //是一个负数
            if (result.length()<minNum.length()){
                return Integer.parseInt(result.toString());
            }else if (result.length()>minNum.length()){
                return Integer.MIN_VALUE;
            }else {
                if (compare(result.toString(),minNum,false)){
                    return   Integer.parseInt(result.toString());
                }else {
                    return Integer.MIN_VALUE;
                }
            }
        }
    }
    public static boolean compare(String s1,String s2,boolean flag){
        int n=s1.length();
        boolean result=true;
        int start=flag?0:1;
        while (start<n){
            int num1=Integer.parseInt(String.valueOf(s1.charAt(start)));
            int num2=Integer.parseInt(String.valueOf(s2.charAt(start)));
            if (num1>num2){
                result=false;
                break;
            }
            start++;
        }
        return result;
    }
}

这个总是有点儿问题,然后是借鉴了别人的代码,如下

class Solution {
   public static int myAtoi(String str) {
        char[] seq=str.trim().toCharArray();
        if(seq.length==0 || (seq[0]<'0' ||seq[0]>'9')&&seq[0]!='-'&&seq[0]!='+') return 0;
        int flag=1;
        int result=0;
        for(int i=0;i<seq.length;i++){
            if(seq[i]=='-' && i==0) {
                flag=-1;
                continue;
            }
            if(seq[i]=='+' && i==0) continue;
            if(seq[i]<'0' ||seq[i]>'9') break;
            if(result*flag>Integer.MAX_VALUE/10||(result*flag==Integer.MAX_VALUE/10&&(seq[i]-'0')>Integer.MAX_VALUE%10)) return Integer.MAX_VALUE;
            if(result*flag<Integer.MIN_VALUE/10||(result*flag==Integer.MIN_VALUE/10&&(seq[i]-'0')*flag<Integer.MIN_VALUE%10)) return Integer.MIN_VALUE;
            result=result*10+seq[i]-'0';
        }
        return result*flag;
    }
}

 

posted @ 2019-09-30 15:03  刘云生  阅读(100)  评论(0编辑  收藏  举报