8. String to Integer (atoi)
来源:互联网 发布:大数据应用方向 编辑:程序博客网 时间:2024/06/03 21:43
题目:
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.
Update (2015-02-10):
The signature of the C++
function had been updated. If you still see your function signature accepts a const char *
argument, please click the reload button to reset your code definition.
本题是字符串转整数,本题要注意的是字符串有正负之分,而且当数值溢出且为正数时,直接输出2147483647,当数值溢出且为负数时,直接输出-2147483648
代码:
class Solution {public: int myAtoi(string str) { long s = 0; bool flag = false; string::iterator itr=str.begin(); while(*itr==' ') { itr++; } if(*itr=='-'||*itr=='+') { if(*itr=='-') flag=true; itr++; } while(*itr>='0'&&*itr<='9') { s=s*10+*itr-'0'; itr++; if(s>=2147483648&&flag==true) { // cout<<s<<endl; s=2147483648; break; } else if(s>=2147483647&&flag==false) { // cout<<s<<endl; s=2147483647; break; } } return (int)s*(flag?-1:1); }};
阅读全文
1 0
- 8.String to Integer (atoi)
- 8. String to Integer (atoi)
- 8. String to Integer (atoi)
- 8.String to Integer (atoi)
- 8. String to Integer (atoi)
- 8. String to Integer (atoi)
- 8. String to Integer (atoi)
- 8. String to Integer (atoi)
- 8. String to Integer (atoi)
- 8. String to Integer (atoi)
- 8. String to Integer (atoi)
- 8. String to Integer (atoi)
- 8. String to Integer (atoi)
- 8. String to Integer (atoi)
- 8. String to Integer (atoi)
- 8. String to Integer (atoi)
- 8. String to Integer (atoi)
- 8. String to Integer (atoi)
- 1750:陶陶摘苹果
- springmvc与Servlet3.0不依赖common包实现文件上传
- RePlugin 常见问题总结
- 「python」常用的数据类型转换
- 云数据库产品及架构设计背后的考量
- 8. String to Integer (atoi)
- [FAQ10563]如何验证APN是否添加成功?
- Docker持续部署图文详解
- Struts2 Part4
- 如何利用思维导图进行写作的6种模板鉴赏
- Spring三种建立数据源的方式(datasource)
- Centos_7.2 安装Mariadb
- gpackcore tool
- 20170904日记账流水(2017-09-04 18:00)