我是靠谱客的博主 繁荣白猫,最近开发中收集的这篇文章主要介绍LeetCode 008 String to Integer (atoi)String to Integer (atoi)题目分析,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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.

题目分析

将字符串转为int
有几点需要注意的是:

  • 字符串前后可能存在空格,应该先丢弃空白字符,找到第一个非空白字符。
  • 然后判断符号,复数有负号。输出时也要输出负号。
  • 若是第一个非空字符不是符号也不是数字,那么直接返回0。
  • 若是超过了int型的上限或者是下限。则输出int型的上下限值。-2147483648 或 2147483647。

代码实在是忍不住用用了网上大神写的,思路太好了,没忍住就抬出来壮壮门面了。好像现在都是贴的人家的程序。咦。这个要注意了以后。

class Solution {
public int myAtoi(String str) {
String strTrim = str.trim(); // remove any leading and trailing whitespace
if (strTrim.length() == 0)
return 0;
int index = 0, sign = 1;
long result = 0;//设置为long型,否则超过上下限就会跳变。无法作比较。
if (strTrim.charAt(index) == '+' || strTrim.charAt(index) == '-') {
sign = strTrim.charAt(index) == '+' ? 1 : -1; // get the sign of the number
index++;
}
// convert string to integer without overflow
while (index < strTrim.length()) {
int digit = strTrim.charAt(index) - '0'; // get the first digit
if (digit < 0 || digit > 9)//当出现了非数字字符,则可以停止了
break;
result = result * 10 + digit; // add the digit to result
if (result > Integer.MAX_VALUE) { // detect the overflow
return sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
}
index++;
}
return (int) (result * sign);
}
}

最后

以上就是繁荣白猫为你收集整理的LeetCode 008 String to Integer (atoi)String to Integer (atoi)题目分析的全部内容,希望文章能够帮你解决LeetCode 008 String to Integer (atoi)String to Integer (atoi)题目分析所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(48)

评论列表共有 0 条评论

立即
投稿
返回
顶部