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.

spoilers alert... click to show requirements for atoi.

Requirements for atoi:

The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.

The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.

If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.

If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.

 

  题目是实现string 转换int 的功能,需要注意的是前置空格,但是会忽略后置部分,需要注意的是判断int 的溢出,可以通过这样的一个判断:
if( res> (INT_MAX - str[i] +'') / )
overflow = ;
end;
 
#include<string>
#include<iostream>
using namespace std; class Solution {
public:
int atoi(string str) {
if(str.length()==) return ;
int len = str.length();
int idx= ;
while(idx<len&&str[idx]==' ') idx++;
if(idx==len) return ;
int flag =;
if(str[idx]=='+') idx++;
else if(str[idx]=='-'){
flag=;
idx++;
}
int sum = ;
int overflow =;
while(idx<len&&str[idx]>=''&&str[idx]<=''){
if(sum>(INT_MAX - str[idx]+'')/){
overflow = ;
break;
}
sum *=;
sum+=str[idx] -'';
idx++;
}
if(overflow){
if(flag) return INT_MAX;
else return INT_MIN;
}
// while(idx<len&&str[idx]==' ') idx++;
// if(idx!=len) return 0;
if(flag) return sum;
else return -sum;
}
}; int main()
{
string str = " 2147483648 ";
Solution sol;
cout<<sol.atoi(str)<<endl;
return ;
}
 
 
 

[LeetCode] String to Integer (atoi) 字符串的更多相关文章

  1. [LeetCode] String to Integer (atoi) 字符串转为整数

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  2. [Leetcode] String to integer atoi 字符串转换成整数

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  3. String to Integer (atoi) - 字符串转为整形,atoi 函数(Java )

    String to Integer (atoi) Implement atoi to convert a string to an integer. [函数说明]atoi() 函数会扫描 str 字符 ...

  4. LeetCode: String to Integer (atoi) 解题报告

    String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...

  5. [LeetCode] 8. String to Integer (atoi) 字符串转为整数

    Implement atoi which converts a string to an integer. The function first discards as many whitespace ...

  6. 【LeetCode】8. String to Integer (atoi) 字符串转换整数

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字符串转整数,atoi,题解,Leetcode, 力扣,P ...

  7. 【LeetCode】8. String to Integer (atoi) 字符串转整数

    题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...

  8. [leetcode]8. String to Integer (atoi)字符串转整数

    Implement atoi which converts a string to an integer. The function first discards as many whitespace ...

  9. 【LeetCode】String to Integer (atoi)(字符串转换整数 (atoi))

    这道题是LeetCode里的第8道题. 题目要求: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们 ...

随机推荐

  1. sql 参数化查询

      在初次接触sql时,笔者使用的是通过字符串拼接的方法来进行sql查询,但这种方法有很多弊端 其中最为明显的便是导致了sql注入. 通过特殊字符的书写,可以使得原本正常的语句在sql数据库里可编译, ...

  2. 浅谈JavaScript字符串拼接

    本文给大家汇总介绍了几种javascript中字符串拼接的方法,十分的简单实用,有需要的小伙伴可以参考下. 在JavaScript中会经常遇到字符串拼接,但是如果要拼接的字符串过长就比较麻烦了. 如果 ...

  3. 数据结构-模式匹配串算法(KMP)

    #include<cstdio> #include<iostream> #include<string> #include<cstring> #incl ...

  4. Serializer序列器

    定义Serializer 1. 定义方法 Django REST framework中的Serializer使用类来定义,须继承自rest_framework.serializers.Serializ ...

  5. linux 查看CPU内存 网络 流量 磁盘 IO

    使用vmstat命令来察看系统资源情况 在命令行方式下,如何查看CPU.内存的使用情况,网络流量和磁盘I/O? Q: 在命令行方式下,如何查看CPU.内存的使用情况,网络流量和磁盘I/O? A: 在命 ...

  6. BFS:Nightmare(可返回路径)

    解题心得: 1.point:关于可以返回路径的BFS的标记方法,并非是简单的0-1,而是可以用时间比较之后判断是否push. 2.queue创建的地点(初始化问题),在全局中创建queue在一次调用B ...

  7. [BZOJ1010]玩具装箱toy(斜率优化)

    Description P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再放到一种特殊的一维容器中.P教授有编号为1... ...

  8. Linux多线程总结

    一.Linux线程 进程与线程之间是有区别的,不过Linux内核只提供了轻量进程的支持,未实现线程模型.Linux是一种“多进程单线程”的操作系统.Linux本身只有进程的概念,而其所谓的“线程”本质 ...

  9. mof格式的文件怎么打开?用什么工具?

    托管对象格式 (MOF) 文件是创建和注册提供程序.事件类别和事件的简便方法.在 MOF 文件中创建类实例和类定义后,可以对该文件进行编译.有关更多信息,请参见编译托管对象格式 (MOF) 文件.编译 ...

  10. 在 Amazon AWS 搭建及部署网站:(三)开发及部署环境

    服务器已经搭建好,网站也开始运行了.那么如何方便地部署代码呢? 最基本的方式,就是使用 SFTP 向网站目录直接部署.这种方法的缺点是版本控制不便,在上传时也无法方便的比较代码变化. 用SVN来部署是 ...