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.

  • 相关阅读:
    C# 将DLL制作CAB包并在浏览器下载,自动安装。(Activex)(包括ie打开cab包一直弹出用户账户控制,确定之后无反应的解决办法。)
    C#解决WebClient不能下载https网页内容
    SQL Server 将两行或者多行拼接成一行数据
    IIS反向代理
    C# HTTP请求对外接口、第三方接口公用类
    SQL SERVER常用语法记录
    SQL SERVER 实现相同记录为空显示(多列去除重复值,相同的只显示一条数据)
    百度编辑器插入视频 实现可实时预览以及支持手机查看
    百度编辑器 动态修改上传图片路径以及上传视频路径
    .Net Core Api发布时报502.5 [The Application process failed to Start]问题的解决原因
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/5139690.html
Copyright © 2011-2022 走看看