[leetcode]String to Integer (atoi)

新博文地址:[leetcode]String to Integer (atoi)

String to Integer (atoi)

 我操,梅西绝杀。。。

一会儿看德国。先把这道题弄一下

 

Implement atoi to convert a string to an integer.

Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.

Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.

spoilers alert... click to show requirements for atoi.

Requirements for atoi:
The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.

The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.

If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.

If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.

 这道题最大的问题就是问题描述不清晰:

希望德国一会儿献上更精彩的比赛。

首先将str开头的空格剪掉,遇到第一个非空格字符之后再处理。

1. 无论什么时候遇到非数字和非符号,处理结束

2. 遇到符号,判断之前有没有遇到数字,如果之前遇到过数字,则退出循环。如果没有遇到数字,再进行这样的判断:

2.1 如果之前还遇到过符号,这是第二次遇到符号,退出循环。

2.2 如果之前没遇到过符号,这是第一次遇到符号,记录该符号。

3. 遇到数字则判断是否会溢出,溢出则做相应处理,否则就记录该数字。

代码如下:

        if(str == null || str.length() == 0 || str.trim().length() == 0){
        	return 0;
        }
        int begin = 0;
        for(begin = 0 ; begin < str.length(); begin++){
        	if(str.charAt(begin) != ' '){
        		break;
        	}
        }
        str = str.substring(begin);
        Set<Character> letterSet = new HashSet<Character>(Arrays.asList('0','1','2','3','4','5','6','7','8','9'));
        Set<Character> signSet = new HashSet<Character>(Arrays.asList('+','-'));
        boolean hasSign = false;
        boolean hasNum = false;
        char[] charArray = str.toCharArray();
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < str.length(); i++){
        	if(!letterSet.contains(charArray[i]) && !signSet.contains(charArray[i])){
        		break;
        	}else if(letterSet.contains(charArray[i])){
        		int rightNow = 0;
        		if(hasNum){
        			rightNow = Integer.valueOf(sb.toString());
        		}
        		if(rightNow > Integer.MAX_VALUE / 10 || (rightNow == Integer.MAX_VALUE / 10 && (charArray[i] - '0') >= Integer.MAX_VALUE % 10 )){
        			return Integer.MAX_VALUE;
        		}else if(rightNow < Integer.MIN_VALUE / 10 || (rightNow == Integer.MIN_VALUE / 10 && (charArray[i] - '0') >= -1 * (Integer.MIN_VALUE % 10) )){
        			return Integer.MIN_VALUE;
        		}else{
        			sb.append(charArray[i]);
        			hasNum = true;
        		}
        	}else if(signSet.contains(charArray[i])){
        		if(hasSign) break;
        		else{
        			if(hasNum) break;
        			hasSign = true;
        			sb.append(charArray[i]);
        		}
        	}
        }
        if(!hasNum){
        	return 0;
        }else{
              if(signSet.contains(sb.charAt(0))){
        	   return sb.charAt(0) == '+' ? Integer.valueOf(sb.substring(1)): -1 * Integer.valueOf(sb.substring(1));
              }else{
        	return Integer.valueOf(sb.toString());
              }
        }
    

 

你可能感兴趣的:(LeetCode)