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

    Update (2015-02-10):
    The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button  to reset your code definition.

    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.

     
    steps:
    1. find the first non-whitespace character
    2. starting from this character, takes an optional inital plus or minus sign
    3. if the next character is a number, convert to int; if not, return the current result value.
    4. consider the overflow. (line 5&16)
     
     1 class Solution {
     2 public:
     3     int myAtoi(string str) {
     4         int i=0;
     5         long long res=0;
     6         int size=str.length();
     7         int sign=1;
     8         if (size==0) return 0; //if string is empty,return 0
     9         while(str[i]==' ') i++; //find the first non-whitespace character
    10         if (str[i]=='+'||str[i]=='-') {//if have the optional sign
    11             sign=(str[i++]=='+')?1:-1;
    12         }
    13         for (;i<size;i++){ 
    14             if (str[i]<='9' && str[i]>='0'){
    15                 res=res*10+str[i]-'0';
    16                 if (res*sign>INT_MAX || res*sign<INT_MIN) return (sign==1)?INT_MAX:INT_MIN;
    17             }
    18             else break;
    19         }
    20         res=sign*res;
    21         return res;
    22     }
    23 };
  • 相关阅读:
    一种不求交点确定直线与三角形是否相交的方法
    碰撞边界锯齿的平滑方法
    demo的凹凸贴图效果
    MySQL进阶篇触发器
    MySQL进阶篇索引
    Maven的POM文件详解
    Swagger
    MySQL进阶篇存储过程
    SpringBoot基础篇
    MySQL基础篇多表操作
  • 原文地址:https://www.cnblogs.com/anghostcici/p/6626241.html
Copyright © 2011-2022 走看看