String to Integer (atoi)

Implement atoi which converts a string to an integer.

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.

Note:

  • Only the space character ' ' is considered as whitespace character.
  • Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231,  231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned.

Example 1:

Input: "42"
Output: 42

Example 2:

Input: "   -42"
Output: -42
Explanation: The first non-whitespace character is '-', which is the minus sign.
             Then take as many numerical digits as possible, which gets 42.

Example 3:

Input: "4193 with words"
Output: 4193
Explanation: Conversion stops at digit '3' as the next character is not a numerical digit.

Example 4:

Input: "words and 987"
Output: 0
Explanation: The first non-whitespace character is 'w', which is not a numerical 
             digit or a +/- sign. Therefore no valid conversion could be performed.

Example 5:

Input: "-91283472332"
Output: -2147483648
Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer.
             Thefore INT_MIN (−231) is returned.

思想:先把正负符号,空格,和0跳过,在循环过程中记录下正负符号的下标,数量和0的开始下标,用于判断正负符号与数字之间的不能有

其他符号,正负符号的数量不能超过一个,符号的前面不能有0.然后找出第一个非0的数字串,判断该数字串长度是否大于10,因为long的长度也有

限制,因此需要先判断在转化成数字。最后根据符号返回转换后的数据。

public static int myAtoi(String str) {
         if(str==null||str.length()==0)
        	 return 0;
         char[] arr = str.toCharArray();
         int len=arr.length;
         int i=0;
         int flag=0;
         int k=-1;
         int m=-1;
         int flag2=0;
         //前面如果是数字符号,空格和0可以出现多次
         while(i<len&&(' '==arr[i]||arr[i]=='+'||arr[i]=='-'||arr[i]=='0')) {
        	
        	 if(arr[i]=='-'||arr[i]=='+') {
        		 flag++;
        		 k=i;//记录符号的起始地址
        	 }
        	 if(arr[i]=='0')
        		 flag2++;
        	 i++;//空格,加号,减号
         }
         //只能有一个+或-号
         if(flag>=2)
        	 return 0;
         //符号后面只能是数字
         m=k;
         while(k!=-1&&(k+1)<len&&arr[k+1]=='0') {
        	 k++;
         }
         //如果符号与数字之间不全是0还有其他非数字符号如+0008返回8或+00 6返回0
         if((k+1)!=i&&k!=-1)
        	 return 0;
         //符号前面不能有0
         k=m;
         if(k!=-1) {
        	 while(k>0) {
        		 if(arr[k-1]=='0')
        			 return 0;
        		 k--;
        	 }
         }
        
         int j=i;
         //不是数字
         if(i>=len)
        	 return 0;
         //不是数字
         if(arr[i]<48||arr[i]>57)
        	 return 0;
         //0和其他数字之间还有其他字符
         k=i;
        while(flag2>0) {
        	 if(arr[k-1]!='0')
        		 return 0;
        	 k--;
        	 flag2--;
         }
         //找数字
         while(i<len&&arr[i]>=48&&arr[i]<=57) {
        	 i++;
         }
         char[] array = Arrays.copyOfRange(arr,j,i);
         String s=new String(array);
         if(s.length()>10&&m>=0&&arr[m]=='-'){
        	 return -2147483648;
         }
         if(s.length()>10&&(m==-1||(m>=0&&arr[m]=='+'))) { 
        	 return 2147483647;
         }
         Long digital=Long.parseLong(s);
         if(digital>2147483647&&(m==-1||(m>=0&&arr[m]=='+')))
        	 return 2147483647;
         if(digital>2147483647&&m>=0&&arr[m]=='-')
        	 return -2147483648;
         if(m>=0&&arr[m]=='-') {
        	 digital=-digital;
         }
         return digital.intValue();
	}

点赞