zoukankan      html  css  js  c++  java
  • String to Integer (atoi)

    https://leetcode.com/problems/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.

     1 public class Solution {
     2     public static int myAtoi(String str) {
     3     if(str.equals("")){return 0;}
     4     long ans=0;
     5     int start=-1;
     6     while(str.charAt(++start)==' ');
     7     str=str.substring(start);
     8     if(str.equals("")){return 0;}
     9     int cout=0;
    10     int fu=1;
    11     int len=str.length();
    12     for(int i=0;i<len;i++){
    13         char c=str.charAt(i);
    14         if(c=='+'||c=='-'){cout++;}
    15         if(cout>1) return 0;
    16         if(c=='-'){fu=-1;}
    17         else if(c=='+');
    18         else if(c>='0'&&c<='9'){
    19         if(ans==0){ ans=c-'0';}
    20         else{ans=ans*10+(c-'0');}
    21         if(ans*fu>Integer.MAX_VALUE||ans*fu<Integer.MIN_VALUE){break;}
    22         }
    23         else{break;}
    24     }
    25     if(ans*fu>Integer.MAX_VALUE){return Integer.MAX_VALUE;}
    26     else if(ans*fu<Integer.MIN_VALUE){return Integer.MIN_VALUE;}
    27     else return (int) (ans*fu);
    28     }
    29     public static void main(String[]args){
    30     String str="9223372036854775809";
    31     System.out.println(myAtoi(str));
    32     }
    33 }
  • 相关阅读:
    memcached
    Springboot配置类
    程序员中的长期主义者-webpack
    程序员中的长期主义者-css
    程序员中的长期主义者
    h5字体样式太小引入UI库字体也很小看不清
    NodeJs正则关键字查找目录文件
    普通二维码跳转微信小程序
    git版本回滚
    绕不过的就突破
  • 原文地址:https://www.cnblogs.com/qq1029579233/p/4477023.html
Copyright © 2011-2022 走看看