[leetcode] String to Integer (atoi)
来源:互联网 发布:淘宝新店扶持政策 编辑:程序博客网 时间:2024/05/22 09:01
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 atoi(const char *str) { if(str==NULL) return 0; if(*str=='\0') return 0; long long res=0; int flag=1; while(*str==' '){ str++; } if(*str=='-'){ flag=-1; str++; } else if(*str=='+'){ str++; } while(isdigit(*str)){ int temp=*str-'0'; res=res*10+temp; if(res*flag>INT_MAX){ res=INT_MAX; break; } if(res*flag<INT_MIN){ res=INT_MIN; break; } str++; } return flag*res; }};
0 0
- LeetCode:String to Integer (atoi)
- LeetCode: String to Integer (atoi)
- LeetCode String to Integer(atoi)
- LeetCode :String to Integer (atoi)
- Leetcode: String to Integer (atoi)
- Leetcode : String to Integer (atoi)
- 【leetcode】String to Integer (atoi)
- [LeetCode]String to Integer (atoi)
- LeetCode-String to Integer (atoi)
- [leetcode] String to Integer (atoi)
- LeetCode - String to Integer (atoi)
- 【leetcode】String to Integer (atoi)
- LeetCode - String to Integer (atoi)
- LeetCode: String to Integer (atoi)
- Leetcode: String to Integer (atoi)
- [LeetCode] String to Integer (atoi)
- leetcode String to Integer (atoi)
- [LeetCode]String to Integer (atoi)
- window7下PHP环境的搭建
- SQL测试试题
- github的使用
- 自动机及其识别的语言对应(小结)
- C# pda 数据采集器 防止休眠
- [leetcode] String to Integer (atoi)
- C++设计模式----观察者模式
- mysql 免安装版配置过程
- windows的磁盘操作之八——格式化分区的思考
- Optimizing Oracle RAC
- 自定义控件之绘图篇(二):路径及文字
- 用最简短的语言解释MapReduce
- 重置mysql主从同步:reset master和reset slave
- jsp中内置对象的四个作用域