string to integer

 1 class Solution {
 2     public:
 3         //计算一个整数的位数,在溢出判断中使用
 4         int bit_length(int n)    {
 5             int l=0;
 6             while(n!=0){
 7                 l++;
 8                 n/=10;
 9             }
10             return l;
11         }
12     int atoi(const char *str){
13         if(*str==‘\0‘) return 0;
14         // 将开头的空白字符和0去掉
15         while(isspace(*str) || *str-‘0‘ ==0 )str++;
16         int number=0;
17         bool times_minus=false;//是否是负数
18         bool symbol=false; //是否违法
19         bool of=false;  //是否溢出
20         while(*str!=‘\0‘){
21             //+号处理
22             if(*str==‘-‘)    {
23                 if(((*(str+1)==‘\0‘ )&&number!=0)|| !isdigit(*(str+1))){
24                     symbol=true;
25                     break;
26                 }
27                 times_minus=true;
28             }
29             //-号处理
30             else if(*str==‘+‘){
31                  if(((*(str+1)==‘\0‘)&&number!=0) || !isdigit(*(str+1))){
32                      symbol=true;
33                      break;
34
35                  }
36               }
37             //数字处理
38             else if(isdigit(*str)){
39                 if(*str-‘0‘==0 && number==0)
40                 {}
41                 else{
42                     int num_copy=number;
43                     number=number*10+*str-‘0‘;
44                     //溢出判断
45                     if(bit_length(number)==bit_length(num_copy)){
46                         of=true;
47                     }
48                 }
49
50             }
51             //其他字符处理
52             else
53                 break;
54
55             str++;
56         }
57
58         if(times_minus==true)
59             number*=-1;
60         //负溢出
61         if (( of&&times_minus==true )|| (number>0 && times_minus==true))
62             number=-2147483648;
63         //正溢出
64         else if((of &&times_minus==false)|| (number<0 && times_minus==false))
65             number=2147483647;
66
67         if(symbol)
68             return 0;
69         else return number;
70     }
71 };

将一个字符串转为一个整数本来是很简单的,但这里要处理的溢出的问题,关于整数的溢出可以参考CSAPP一书(汗,基本上参考的数目都是CSAPP、SICP、CLRS。。。不过这几本书都是神书级别,看了就会有很大提高的好书)。
题目要求

1. 字符串开头可以有空白字符;

2. 字符串里面出现不合法的字符就从这个截断;

3. +号和负号只能出现在数字的开头,且只有一个;

4. 数字溢出后按照32位整数表示来处理;这一步是重点,我结合了位数变换和操作数的符号来综合判断。

5. 数字开头可能有0,也需要滤去

时间: 2024-10-10 17:08:34

string to integer的相关文章

LeetCode:String to Integer (atoi)

1.题目名称 String to Integer (atoi) (字符串到数字的转换) 2.题目地址 https://leetcode.com/problems/string-to-integer-atoi/ 3.题目内容 英文:Implement atoi to convert a string to an integer. 中文:实现atoi函数,将输入的字符串(String类型)转换为整型数据(Integer类型) 提示:实现的atoi函数需要满足以下特征 忽略字符串第一个非空格字符前的所

Leetcode 数 String to Integer (atoi)

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie String to Integer (atoi) Total Accepted: 9862 Total Submissions: 67880 Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge,

【leetcode】String to Integer (atoi)

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 f

【LeetCode】- String to 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). Y

[LeetCode][JavaScript]String to Integer (atoi)

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 f

LeetCode【8】. String to Integer (atoi) --java实现

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 f

LeetCode 008 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 b

每日算法之八: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 spe

【Leet Code】String to Integer (atoi) ——常考类型题

String to Integer (atoi) Total Accepted: 15482 Total Submissions: 106043My Submissions 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 yours

leetcode——String to Integer (atoi) 字符串转换为整型数(AC)

mnesia在频繁操作数据的过程可能会报错:** WARNING ** Mnesia is overloaded: {dump_log, write_threshold},可以看出,mnesia应该是过载了.这个警告在mnesia dump操作会发生这个问题,表类型为disc_only_copies .disc_copies都可能会发生. 如何重现这个问题,例子的场景是多个进程同时在不断地mnesia:dirty_write/2 mnesia过载分析 1.抛出警告是在mnesia 增加dump