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

    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. 开头有任意多个空格,需要trim
    2. 数字开头可能有符号,+,—
    3. 输入可能不是一个整数,如12dds返回12;abc返回0
    4. 输入整数可能会超过整数表示范围,如超过则返回 INT_MAX (2147483647) or INT_MIN (-2147483648)
     1 public class Solution {
     2     public int atoi(String str) {
     3         // Start typing your Java solution below
     4         // DO NOT write main() function
     5         str = str.trim();
     6         String[] integers = str.split(" ");
     7         if(integers[0].equals(""))
     8             return 0;
     9         String s1 = integers[0];
    10         boolean positiveFlag = true;
    11         if(s1.charAt(0) == '+'){
    12             s1 = s1.substring(1);
    13         } else if(s1.charAt(0) == '-'){
    14             positiveFlag = false;
    15             s1 = s1.substring(1);
    16         } 
    17         long result = 0;
    18         for(int i = 0; i < s1.length(); i++){
    19             if(s1.charAt(i) >= 48 && s1.charAt(i) <= 57) 
    20                 result = result * 10 + (s1.charAt(i) - 48);
    21             else
    22                 break;
    23         }
    24         
    25         if(!positiveFlag)
    26             result = result * -1;
    27             
    28         if(result > 2147483647)
    29             return 2147483647;
    30             
    31         if(result < -2147483648)
    32             return -2147483648;
    33             
    34         return (int)result;    
    35         
    45     }
    46 }

     refactor code:

     1 public int atoi(String str) {
     2         // Note: The Solution object is instantiated only once and is reused by each test case.
     3         if(str == null || str.length() == 0){
     4             return 0;
     5         }
     6         
     7         str = str.trim();
     8         boolean positiveFlag = true;
     9         if(str.charAt(0) == '-'){
    10             positiveFlag = false;
    11             str = str.substring(1);
    12         } else if(str.charAt(0) == '+'){
    13             positiveFlag = true;
    14             str = str.substring(1);
    15         }
    16         
    17         long result = 0;
    18         for(int i = 0; i < str.length(); i++){
    19             char c = str.charAt(i);
    20             if(c >= '0' && c <= '9'){
    21                 result = 10 * result + (c - '0');
    22             } else {
    23                 break;
    24             }
    25         }
    26         if(!positiveFlag){
    27             result = -1 * result;
    28         }
    29         
    30         if(result < Integer.MIN_VALUE){
    31             return Integer.MIN_VALUE;
    32         }
    33         if(result > Integer.MAX_VALUE){
    34             return Integer.MAX_VALUE;
    35         }
    36         return (int)result;
    37     }
  • 相关阅读:
    深度分析:java8的新特性lambda和stream流,看完你学会了吗?
    花了三天整理,Spring Cloud微服务如何设计异常处理机制?还看不懂算我输
    做了两年java,这些高性能高可用高并发的技术架构你都知道吗?
    面试阿里,字节跳动90%会被问到的微服务,你确定不进来看看吗?
    阿里面试官:小伙子,你给我说一下前后端分离的接口规范是什么?
    深度分析:面试阿里,字节跳动,美团几乎都会被问到的阻塞队列
    1. 线性DP 1143. 最长公共子序列
    1. 线性DP 300. 最长上升子序列 (LIS)
    GC 的认识(转) https://github.com/qcrao/Go-Questions/blob/master/GC/GC.md#1-什么是-gc有什么作用
    缓存淘汰算法--LRU算法
  • 原文地址:https://www.cnblogs.com/feiling/p/3158218.html
Copyright © 2011-2022 走看看