LeetCode:String to Integer (atoi)

简介:

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.

 

实现字符串转整数的函数,需要注意的几点: 本文地址

1、对于非法输入,函数输出0(除了溢出)

2、处理输入NULL

3、处理数字前面的空格

4、处理数字前面的0

5、处理溢出

6、注意“+ 123”输入是非法的

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class  Solution {
public :
     int  atoi ( const  char  *str) {
         if (str == NULL) return  0; //0、处理NULL
         while (*str == ' ' )str++; //1、过滤掉空格
         bool  sign = true ;
         if (*str == '-' ){sign = false ; str++;} //2、判断符号位
         else  if (*str == '+' )str++;
         while (*str == '0' )str++; //3、过滤掉数字前面的0
         
         long  long  res = 0, lastRes = 0;
         while (*str != '\0'  && *str >= '0'  && *str <= '9' )
         {
             lastRes = res;
             res = res*10 + ( int )(*str - '0' );
             if (res > INT_MAX) return  sign == true  ? INT_MAX : INT_MIN; //4、判断是否溢出
             str++;
         }
         
         return  sign == true  ? res : res*-1;
     }
};

 

上面是把res声明为longlong来判断溢出,亦可以如下操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class  Solution {
public :
     int  atoi ( const  char  *str) {
         if (str == NULL) return  0; //0、处理NULL
         while (*str == ' ' )str++; //1、过滤掉空格
         bool  sign = true ;
         if (*str == '-' ){sign = false ; str++;} //2、判断符号位
         else  if (*str == '+' )str++;
         while (*str == '0' )str++; //3、过滤掉数字前面的0
         
         int  res = 0, lastRes = 0;
         int  limit = INT_MAX / 10;
         while (*str != '\0'  && *str >= '0'  && *str <= '9' )
         {
             if (limit < res) return  sign == true  ? INT_MAX : INT_MIN; //4、判断是否溢出(两个数相乘溢出后,溢出的结果不一定比两个乘数小)
             lastRes = res;
             res = res*10 + ( int )(*str - '0' );
             if (res < lastRes) return  sign == true  ? INT_MAX : INT_MIN; //4、判断是否溢出
             str++;
         }
         
         return  sign == true  ? res : res*-1;
     }
};





本文转自tenos博客园博客,原文链接:http://www.cnblogs.com/TenosDoIt/p/3681845.html,如需转载请自行联系原作者

目录
相关文章
|
4月前
|
算法 C++
【LeetCode】【C++】string OJ必刷题
【LeetCode】【C++】string OJ必刷题
30 0
存储 编译器 Linux
18 0
|
5月前
|
Java
Leetcode 467. Unique Substrings in Wraparound String
大概翻译下题意,有个无限长的字符串s,是由无数个「abcdefghijklmnopqrstuvwxy」组成的。现在给你一个字符串p,求多少个p的非重复子串在s中出现了?
19 0
|
9月前
|
算法 C++ Python
【力扣算法11】之 8. 字符串转换整数 (atoi) python
【力扣算法11】之 8. 字符串转换整数 (atoi) python
79 0
|
10月前
|
存储 算法 测试技术
力扣7-整数反转&力扣8-字符串转换整数 (atoi)
力扣7-整数反转&力扣8-字符串转换整数 (atoi)
64 0
|
10月前
|
算法 安全 Swift
LeetCode - #8 字符串转换整数(atoi)
不积跬步,无以至千里;不积小流,无以成江海,Swift社区 伴你前行。如果大家有建议和意见欢迎在文末留言,我们会尽力满足大家的需求。
|
10月前
|
存储 测试技术
leetcode:8.字符串转换正数(atoi)
当我们寻找到的第一个非空字符为正或者负号时,则将该符号与之后面尽可能多的连续数字组合起来,作为该整数的正负号;假如第一个非空字符是数字,则直接将其与之后连续的数字字符组合起来,形成整数。
27 0
|
10月前
力扣 8. 字符串转换整数 (atoi) 解题
力扣 8. 字符串转换整数 (atoi) 解题
56 0
|
11月前
|
算法 C++
模拟实现atoi函数(将数字字符串转换为整型)附加leetcode练习题
各位朋友们,大家好啊!今天我为大家分享的知识是如何模拟实现atoi函数。相信大家如果能够理解这个知识,对大家以后的刷题是有帮助的。
|
11月前
|
缓存 JSON NoSQL
Map<Integer,Value>放入缓存后取出来变成了Map<String,Value>
Map<Integer,Value>放入缓存后取出来变成了Map<String,Value>
161 0