leetceode_8_String to Integer (atoi)_(C++)(easy)
来源:互联网 发布:python 执行adb 编辑:程序博客网 时间:2024/06/05 05:03
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.
思路:主要考察边界条件的测试。字符串前几个若为空格则跳过。加减符号另行考虑。且正数的最大界与负数的最小界也要考虑进去。正负数时对于前一次运行结果分别使用加减。
class Solution {
public:
int myAtoi(string str) {
bool g_nStatus = false;
const char* digit = str.c_str();
long long num = 0;
if(digit == NULL)
return false;
else
{
while(*digit == ' ')
digit++;
bool minus = false;
if(*digit == '+')
digit++;
else if(*digit == '-')
{
digit++;
minus = true;
}
while(*digit != '\0')
{
if(*digit >= '0' && *digit <='9')
{
num = num * 10 + (*digit - '0');
if(num > std::numeric_limits<int>::max())
{
num = 0;
break;
}
digit++;
}
else
{
num = 0;
break;
}
}
if(*digit != NULL)
{
g_nStatus = true;
if(minus)
num = 0 - num;
}
}
return num;
}
};
- leetceode_8_String to Integer (atoi)_(C++)(easy)
- Leetcode[easy] - String to Integer (atoi)
- 8. String to Integer (atoi) [easy] (Python)
- Easy 8题 String to Integer (atoi)
- [C++] String to Integer (atoi)
- leetcode_13_Roman to Integer(C++)(easy)
- LeetCode-8-String to Integer (atoi) (String/Num)-Easy
- Leet Code OJ 8. String to Integer (atoi) [Difficulty: Easy]
- Easy-题目67:8. String to Integer (atoi)
- Leetcode 8. String to Integer (atoi) (Easy) (cpp)
- Leet Code OJ 8. String to Integer (atoi) [Difficulty: Easy]
- [C++]LeetCode: 24 String to Integer (atoi)
- 【leetcode c++】08 String to Integer (atoi)
- 008String to Integer (atoi) (C)
- Leetcode c语言-String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- JAVA面试题
- VB随机生成10个数并排序
- 从txt里面读文件并逐行输出到控制台
- java实现打印菱形
- C/C++中const关键字详解
- leetceode_8_String to Integer (atoi)_(C++)(easy)
- BPM中常见子流程使用场景介绍二(内嵌型、事务型、事件型)
- sdutoj 3361 数据结构实验之图论四:迷宫探索(dfs)
- 修改git全部已提交的用户名和邮箱
- 如何在string.xml的内容中添加空格
- cocoapod创建自己的私有库
- 追追追
- 装饰者模式
- linux基础(20)--linux磁盘阵列RAID--RHEL6.5