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 }
  • 相关阅读:
    工业互联网兴起
    互联网经济学
    广泛应用的区块链技术
    工业互联网数据传输探讨
    谈谈网站性能
    深入探讨vue响应式原理
    工业互联网虚拟数字
    对www.518shengmao.com站资源打包,采用vue Node.js
    jquery的事件命名空间详解
    巧用索引与变量
  • 原文地址:https://www.cnblogs.com/reynold-lei/p/3374113.html
Copyright © 2011-2022 走看看