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

    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.

    这道题主要考虑

    1.字符串最前面的空格舍弃str.trim()

    2.正负号str.charAt(0)

    3.无效字符直接break

    4.溢出可以用long保存,和2147483647(Java int最大值)进行比较

    自己A没想那么多,写的有点乱

     1 package com.gxf.leetcode;
     2 
     3 public class Solution {
     4     
     5     public int  atoi(String str) {
     6         String result = "";
     7         long result_int = 0;
     8         boolean flag_neg = false;
     9         boolean flag_over = false;
    10         if(null == str || 0 == str.length())
    11             return 0 ;
    12         char array_char[] = str.toCharArray();
    13         //消除空格
    14         for(int i = 0; i < array_char.length && array_char[i] == ' '; i++){
    15             str = str.substring(1, str.length());
    16         }
    17         array_char = str.toCharArray();
    18         if(array_char[0] == '-'){
    19             flag_neg = true;
    20             str = str.substring(1, str.length());
    21         }
    22         if('+' == array_char[0]){
    23             str = str.substring(1, str.length());
    24         }
    25         array_char = str.toCharArray();
    26         for(int i = 0; i < array_char.length; i++){
    27             if(array_char[i]>= '0' && array_char[i] <= '9'){//后面的前移
    28                 result_int = (result_int * 10 + (array_char[i] - '0'));
    29                 if(result_int > 2147483647){//正数溢出,变负数
    30                     flag_over = true;
    31                 }
    32                 
    33             }
    34             else if(array_char[i] == ' '){
    35                 break;
    36             }
    37             else{
    38                 //test = 0;
    39                 break;
    40             }
    41         }
    42         if(flag_neg && flag_over)
    43             return -2147483648;
    44         if(!flag_neg && flag_over)
    45             return 2147483647;
    46         if(flag_neg)
    47             return -(int)result_int;
    48         return (int)result_int;
    49     }
    50     
    51 }

    CSDN上面有个写的要好一点http://blog.csdn.net/beiyetengqing/article/details/8458725代码我也偷过来了

     1 public class Solution {
     2     public int atoi(String s) {
     3         s = s.trim();
     4         long value = 0;
     5         boolean isPositive = true;
     6         
     7         for (int i = 0; i < s.length(); i++) {
     8             if (i == 0 && (s.charAt(i) == '+' || s.charAt(i) == '-')) {
     9                 if (s.charAt(i) == '-') {
    10                     isPositive = false;
    11                 }
    12                 continue;
    13             }
    14             if (s.charAt(i) < '0' || s.charAt(i) > '9') break;
    15             value = 10 * value + s.charAt(i) - '0';
    16         }
    17         value = isPositive == true ? value : value * -1;
    18         if (value < -2147483648) {
    19             return -2147483648;
    20         } else if (value > 2147483647) {
    21             return 2147483647;
    22         } else {
    23             return (int) value;
    24         }
    25     }
    26 }

     

  • 相关阅读:
    [CF590C] Three States
    [CF767B] The Queue
    [CF1296F] Berland Beauty
    [CF3D] Least Cost Bracket Sequence
    YUV420 转 RGB 测试
    [POI2012] TOU-Tour de Byteotia
    [CF576C] Points on Plane
    [CF191C] Fools and Roads
    [CF1485C] Floor and Mod
    [CF1399D] Binary String To Subsequences
  • 原文地址:https://www.cnblogs.com/luckygxf/p/4086079.html
Copyright © 2011-2022 走看看