Q8:String to Integer (atoi)

8. String to Integer (atoi)

官方的链接:8. String to Integer (atoi)

Description :

Implement atoi to convert a string to an integer.

Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.

Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.

Requirements for atoi:

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. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.

 

问题描述

 atoi (表示 ascii to integer)是把字符串转换成整型数的一个函数,函数会扫描参数nptr字符串,跳过前面的空白字符(例如空格)等,直到遇上数字或正负符号才开始做转换,而在遇到非数字或字符串结束时才结束转换,并将结果返回。如果 nptr不能转换成 int、nptr为空字符串,那么将返回 。溢出则返回INT_MAX或者INT_MIN。

思路

 一开始理解有误,以为是各种字符都有。后来参考了下官网的Discuss,发现大致为:跳过空格,选择可选的前导+或者-,后面是尽可能多的数字,直到结束或者遇到非数字。以下的代码就当是一种参考。

项目中确实遇到过这种场景:数值型的字段使用正负号+定长字符,然后对字符串进行解析。 这种场景下会有正负号、符号后的0和实际的数字,比如定长10位,第1位是正负号:+066666660,实际上就是66666660。

[github-here]

 1 public class Q8_StringToInteger {
 2     public int myAtoi(String str) {
 3         if (null == str || str.isEmpty()) {
 4             return 0;
 5         }
 6         int result = 0, sign = 1, index = 0, len = str.length();
 7         while (index < len && str.charAt(index) == ' ') {
 8             index++;
 9         }
10         if (index == len) {
11             return 0;
12         }
13         if (str.charAt(index) == '+' || str.charAt(index) == '-') {
14             sign = str.charAt(index++) == '-' ? -1 : 1;
15         }
16         while (index < len && str.charAt(index) <= '9' && str.charAt(index) >= '0') {
17             //溢出判断
18             if (result > Integer.MAX_VALUE / 10 || (result == Integer.MAX_VALUE / 10 && str.charAt(index) - '0' > 7)) {
19                 return sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
20             }
21             result = result * 10 + str.charAt(index++) - '0';
22         }
23         return result * sign;
24     }
25 }

 

转载于:https://www.cnblogs.com/wpbxin/p/8660518.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值