首页 > 其他 > 详细

leetcode 刷题记录-持续更新

时间:2019-09-03 19:12:01      阅读:91      评论:0      收藏:0      [点我收藏+]
8. String to Integer (atoi)
Medium

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.
Accepted
408,261
Submissions
2,761,055

 

 1 public static int myAtoi(String str) {
 2 
 3             // 1字符串非空判断 ""||" "
 4             if (str.isEmpty() || str.trim().isEmpty()) {
 5                 return 0;
 6             }
 7 
 8             int index = 0;
 9             int sign = 1;
10             int total = 0;
11             //1检测第一个非空字符串是什么
12             while (str.charAt(index) == ‘ ‘ && index < str.length()) {
13                 index++;
14             }
15 
16             //1判断这个数是正数还是负数
17             if (str.charAt(index) == ‘+‘ || str.charAt(index) == ‘-‘) {
18                 sign = str.charAt(index) == ‘+‘ ? 1 : -1;
19                 index++;
20             }
21             
22             //1判断是否是数字,是否越界,如果越界就取越界的边界值
23             while (index < str.length()) {
24                 int digit = str.charAt(index) - ‘0‘;
25                 if (digit < 0 || digit > 9) {
26                     break;
27                 }
28 
29                 if (Integer.MAX_VALUE / 10 > total
30                         || (Integer.MAX_VALUE / 10 == total && Integer.MAX_VALUE % 10 >= digit)) {
31                     total = total * 10 + digit;
32                     index++;
33                 } else {
34                     return sign > 0 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
35                 }
36             }
37             return total * sign;
38 
39         }

 

 

技术分享图片

 

leetcode 刷题记录-持续更新

原文:https://www.cnblogs.com/xiaoshahai/p/11454298.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!