String to Integer (atoi)(算法)

做了一早上的题…… 因为考虑不周全所以一直在改判断逻辑。最终Accept的代码只超过了1.28%的java提交内容……

(。・∀・)ノ゙嗨……心情很挫败。但至少又学会了点东西。打算先把自己的记录下,再贴一下别人的高效方法/(ㄒoㄒ)/~~

ADD:

好吧,去掉了System.out.println的语句之后:

  Your runtime beats 18.36% of javasubmissions.

 ~~o(>_<)o ~~

题目

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.

My Ugly Code

 1 public class Solution {
 2     public int myAtoi(String str) {
 3       if(str==null || str.equals(""))
 4             return 0;
 5         str = str.trim();
 6         boolean symb = true;//true+ false-
 7         int i=0;//To ignore the symbol digit
 8         if(str.charAt(0)=='+' ||str.charAt(0)=='-'){
 9             i++;
10             if(str.charAt(0)=='-')
11                 symb = false;
12         }
13         int ret = 0;
14         for(;i<str.length();i++){
15             if(Character.isDigit(str.charAt(i))){
16                 if(ret>(Integer.MAX_VALUE/10) || i==11)//str.charAt(i)>'7')
17                     return symb?Integer.MAX_VALUE:Integer.MIN_VALUE;
18                 ret = ret*10 + str.charAt(i)-'0';
19                 System.out.println(symb+"  "+ret+" "+str.charAt(i)); //去掉这句时间变快了无数倍!!!
20   if(symb && (ret<0))//overflow 21 return Integer.MAX_VALUE; 22 else if(!symb && (ret<0)) 23 return Integer.MIN_VALUE; 24 } 25 else 26 break; 27 } 28 return symb?ret:(-1)*ret; 29 } 30 }

改进后的我的Code

加粗部分就是改进部分……

 1 public class Solution {
 2     public int myAtoi(String str) {
 3       if(str.isEmpty())
 4             return 0;
 5         str = str.trim();
 6         boolean symb = true;//true+ false-
 7         int i=0;//To ignore the symbol digit
 8         if(str.charAt(0)=='+' ||str.charAt(0)=='-'){
 9             i++;
10             if(str.charAt(0)=='-')
11                 symb = false;
12         }
13         int ret = 0;
14         for(;i<str.length();i++){
15             if(Character.isDigit(str.charAt(i))){
16                 if(ret>(Integer.MAX_VALUE/10) || 
17                     (Integer.MAX_VALUE/10==ret && (str.charAt(i)-'0')>Integer.MAX_VALUE%10))
18                     return symb?Integer.MAX_VALUE:Integer.MIN_VALUE;
19                 else
20                     ret = ret*10 + str.charAt(i)-'0';
21             }
22             else
23                 break;
24         }
25         return symb?ret:(-1)*ret;
26     }
27 }

 

posted @ 2016-07-08 15:05  静静静流  阅读(205)  评论(0编辑  收藏  举报