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

     1 class Solution08 {
     2     public int myAtoi(String str) {
     3         if (str.length() == 0) return  0;
     4         String result = new String("");
     5         int i;
     6         for (i = 0; i < str.length(); i++) {//去空格
     7             if (str.charAt(i) != ' ')
     8                 break;
     9         }
    10         for (; i < str.length(); i++) {
    11             if (Character.isDigit(str.charAt(i))){//开头为数字
    12                 result += str.charAt(i);
    13             }else if (str.charAt(i) == '-' && result != ""){//开头为负号
    14                 result += "-";
    15             }else if (str.charAt(i) == '+' && result != ""){//开头为正号
    16                 result += "+";
    17             }else {
    18                 break;
    19             }
    20         }
    21         if (result.equals(""))
    22             return 0;
    23         if (result.length() >= 2 && !Character.isDigit(result.charAt(1)))
    24             return 0;
    25         if (result.equals("-") || result.equals("+")){
    26             return 0;
    27         }
    28 
    29         // -6----的情况
    30         for (int j = 1; j < result.length(); j++) {
    31             if (result.charAt(j) == '-' || result.charAt(j) == '+'){
    32                 result = result.substring(0,j);
    33                 break;
    34             }
    35         }
    36         System.out.println("result = " + result);
    37         try {
    38             int res = Integer.valueOf(result);
    39             return res;
    40         }catch (NumberFormatException e){
    41             if (result.charAt(0) == '-')
    42                 return -2147483648;
    43             else
    44                 return 2147483647;
    45         }
    46     }
    47 }
  • 相关阅读:
    常用数据结构之哈希表
    常用数据结构之队列
    常用的数据结构之栈
    常用的数据结构之链表
    Zabbix3.4监控Windows机器CPU使用率
    在Pycharm中导入第三方模块库(诸如:matplotlib、numpy等)
    WARNING: You are using pip version 20.2.4; however, version 20.3.1 is available.
    npm无法安装node-sass的解决方法
    常见的树形结构封装
    Mac安装MySql
  • 原文地址:https://www.cnblogs.com/yfs123456/p/10918689.html
Copyright © 2011-2022 走看看