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.

  • 相关阅读:
    linux中RabbitMQ安装教程
    linux中的文件权限chmod
    ceph架构简介
    利用双重检查锁定和CAS算法:解决并发下数据库的一致性问题
    对接第三方服务引起的小思考-回调和Sign算法
    <<Java并发编程的艺术>>-阅读笔记和思维导图
    SpringBoot2+Netty打造通俗简版RPC通信框架(升级版)
    SpringBoot2+Netty打造通俗简版RPC通信框架
    [安全] Kali Linux (debian)系统使用记录
    [安全] nmap工具的使用
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/5139690.html
Copyright © 2011-2022 走看看