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

    原题链接在这里:https://leetcode.com/problems/string-to-integer-atoi/

    题目:

    Implement atoi which converts a string to an integer.

    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.

    Note:

    • Only the space character ' ' is considered as whitespace character.
    • Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231,  231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned.

    Example 1:

    Input: "42"
    Output: 42
    

    Example 2:

    Input: "   -42"
    Output: -42
    Explanation: The first non-whitespace character is '-', which is the minus sign.
                 Then take as many numerical digits as possible, which gets 42.
    

    Example 3:

    Input: "4193 with words"
    Output: 4193
    Explanation: Conversion stops at digit '3' as the next character is not a numerical digit.
    

    Example 4:

    Input: "words and 987"
    Output: 0
    Explanation: The first non-whitespace character is 'w', which is not a numerical 
                 digit or a +/- sign. Therefore no valid conversion could be performed.

    Example 5:

    Input: "-91283472332"
    Output: -2147483648
    Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer.
                 Thefore INT_MIN (−231) is returned.

    题解:

    先把前后的空格去掉,然后取首个符号位.

    Note: overflow, 若是res已经大于Intger.MAX_VALUE/10, res*10就会溢出. 若是res == Integer.MAX_VALUE/10, 但最后一位比8大都会溢出,因为Integer的范围是 -2147483648 到 2147483647, 最后一位若是8的话就会溢出, 因为Integer.MAX_VALUE 的最后一位是7.

    Time Complexity: O(str.length()). Space: O(1).

    AC Java:

     1 class Solution {
     2     public int myAtoi(String str) {
     3         if(str == null){
     4             return 0;
     5         }
     6         
     7         // 去掉前后空格
     8         // then check if it is empty, corner case " "
     9         str = str.trim();
    10         if(str.length() == 0){
    11             return 0;
    12         }
    13         
    14         //首个char是符号
    15         boolean isNeg = false;
    16         int i = 0;
    17         if(str.charAt(0) == '+'){
    18             i++;
    19         }else if(str.charAt(0) == '-'){
    20             isNeg = true;
    21             i++;
    22         }
    23         
    24         int res = 0;
    25         while(i<str.length() && Character.isDigit(str.charAt(i))){
    26             char c = str.charAt(i);
    27             // Overflow
    28             if(res > Integer.MAX_VALUE/10 || (res == Integer.MAX_VALUE/10 && c>='8')){
    29                 return isNeg ? Integer.MIN_VALUE : Integer.MAX_VALUE;
    30             }
    31             
    32             res = res*10 + (c-'0');
    33             i++;
    34         }
    35         
    36         return isNeg ? -res : res;
    37     }
    38 }

    类似Reverse Integer.

  • 相关阅读:
    Python可视化库Matplotlib绘图基础学习
    字典特征和文本特征数据抽取
    ipc_11_快乐 happy
    关于Scanf的返回值问题
    [转]网站性能测试总结
    C语言运算符优先级
    成员运算符(·)和指向结构体成员运算符(->)的区别
    c++抛出异常与栈展开(stack unwinding)
    What is a Delegate?
    1.2 Variables and Arithmetic Expressions
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/5139690.html
Copyright © 2011-2022 走看看