zoukankan      html  css  js  c++  java
  • 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.

     1 public class Solution {
     2     public int atoi(String str) {
     3         // Note: The Solution object is instantiated only once and is reused by each test case.
     4         int num = 0;
     5         int sig = 1;
     6         if(str == null || str.length() == 0) return num;
     7         int len = str.length();
     8         int i = 0;
     9         while(str.charAt(i) == ' ' && i < len){
    10             i ++;
    11         }
    12         if(str.charAt(i) == '+') i ++;
    13         if(str.charAt(i) == '-'){
    14             sig = -1;
    15             i ++;
    16         }
    17         for(; i < len; i ++){
    18             int c = str.charAt(i) - '0';
    19             if(c < 0 || c > 9) break;
    20             if(Integer.MAX_VALUE / 10 < num || (Integer.MAX_VALUE / 10 == num && Integer.MAX_VALUE % 10 < c)){
    21                 return sig == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
    22             }
    23             num = num * 10 + c;
    24         }
    25         return num * sig;
    26     }
    27 }

     第三遍:

     1 public class Solution {
     2     public int atoi(String str) {
     3         if(str == null || str.length() == 0) return 0;
     4         int len = str.length();
     5         int sig = 1, result = 0;
     6         int i = 0;
     7         while(str.charAt(i) == ' ') i ++;
     8         if(i < len && (str.charAt(i) == '+' || str.charAt(i) == '-')){
     9             sig = str.charAt(i) == '+' ? 1 : -1;
    10             i ++;
    11         }
    12         for(;i < len && str.charAt(i) >= '0' && str.charAt(i) <= '9'; i++){
    13             int num = str.charAt(i) - '0';
    14             if(result < Integer.MAX_VALUE / 10 || (result == Integer.MAX_VALUE / 10 && num <= Integer.MAX_VALUE % 10 ))
    15                 result = result * 10 + num;
    16             else
    17                 return sig == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
    18         }
    19         return sig * result;
    20     }
    21 }
  • 相关阅读:
    Mysql创建自定义函数
    本草纲目之五味四气
    linux svn命令
    linux命令提升
    php isset缺陷 用array_key_exists
    jquery之ajax
    简单的小游戏(猜数字)
    小球上下左右移动
    如果想在输出面板中排列出一个乘法口诀表请用以下方法
    并联电路
  • 原文地址:https://www.cnblogs.com/reynold-lei/p/3374113.html
Copyright © 2011-2022 走看看