LeetCode8 String to Integer (atoi)

题意:

Implement atoi to convert a string to an integer.  (Easy)

分析:

就是注意各种特殊情况,边界情况的判断,见代码注释。

 1 class Solution {
 2 public:
 3     int myAtoi(string str) {
 4         int start = 0;
 5         long long result = 0;
 6         int flag = 0;
 7         //开头多余空格的处理
 8         while (str[start] == ‘ ‘) {
 9             start++;
10         }
11         if (str[start] == ‘+‘) {
12             start++;
13         }
14         // “+-2”情况, else if...
15         else if (str[start] == ‘-‘) {
16             start++;
17             flag = 1;
18         }
19         //一开始就出现非法字符
20         if (str[start] < ‘0‘ || str[start] > ‘9‘) {
21             return 0;
22         }
23         //+-号处理完毕后开始多余0的处理
24         while (str[start] == ‘0‘) {
25             start++;
26         }
27         for (int i = start; i < str.size(); ++i) {
28             //后续可以有多余数字
29             if (str[i] < ‘0‘ || str[i] > ‘9‘) {
30                 return (flag == 0) ? result : -result;
31             }
32             int temp = str[i] - ‘0‘;
33             //用这种方式,不要用pow(x,y)
34             result = result * 10 + temp;
35             //溢出的处理
36             if (result > 0x7FFFFFFF ) {
37                 return  (flag == 0) ? 0x7FFFFFFF : 0x80000000;
38             }
39         }
40         if (flag == 0) {
41             return result;
42         }
43         else {
44             return -result;
45         }
46
47     }
48 };
时间: 2024-08-08 09:42:03

LeetCode8 String to Integer (atoi)的相关文章

LeetCode8——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

[JAVA]LeetCode8 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

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][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