LeetCode String to Integer (atoi)
来源:互联网 发布:游戏制作软件大全 编辑:程序博客网 时间:2024/05/16 12:09
剑指offer倒数第二道题,用long long 来存整数才能判断相乘是否溢出
#include "stdafx.h"#include <iostream>#include <string>using namespace std;int atoi(const char *str) {int i,signal=1;long long sum=0;if (str==NULL||*str==0){return 0;}while(*str==' '||*str=='\t'||*str=='\n'){str++;}if (*str=='-'||*str=='+'){if (*str=='-'){signal=-1;}str++;}if (*str>'9'||*str<'0'){return 0;}while(*str){if (*str>'9'||*str<'0'){return sum;}if (signal<0){if (10*sum < INT_MIN){return INT_MIN;}sum = sum*10 - *str + '0';//sum*10 + -( *str - '0')if (sum < INT_MIN){return INT_MIN;//-2147483648;-1;}}else{if ( 10 * sum > INT_MAX)//if (10*sum<0){return INT_MAX;}sum = sum*10 + *str - '0';//sum*10 + ( *str - '0')if (sum > INT_MAX){return INT_MAX;//(2147483647);}}str++;}return sum;}int _tmain(int argc, _TCHAR* argv[]){char s[100]=" 10522545459";cout<<atoi(s)<<endl;system("pause");return 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)
- 死锁
- notify();wait();notifyALL();
- 输入输出最终修改
- QT5.1:-1: 错误:INTERNAL: readdir: Invalid argument
- 数据库关联映射建表图示(一对多、多对多)
- LeetCode String to Integer (atoi)
- 奇妙的变化
- 登陆用户怎样获取验证码和保存用户到cookie中
- Fibonacci数列问题
- RibbonApplicationMenu - Label 设置不启作用
- conflicting types for ...
- NSMutableArray的正确使用
- ndk开发过程中中文乱码问题的解决方案
- ubuntu 切换到 root 身份