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.

  • 相关阅读:
    Hibernate实体对象三种状态
    tar命令: 对某目录文件打tar包时,排除指定的目录或文件
    开发项目时,提示 找不到类的解决方法,以及如何设置编译源目录
    当html中存在url中如: onclick="toView('参数1')", 参数1是特别字符,如&asop;&quot;' "等时,浏览器解析时会报错。解决方法如文中描述
    oracle表分区心得
    启动系统相关服务笔记整理
    使用PSD设计网页页面
    JAR、WAR、EAR 区别
    设置 MyEclipse 默认打开文件方式
    前端性能优化
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/5139690.html
Copyright © 2011-2022 走看看