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. java util - 在java代码中执行javascript代码工具 rhino-1.7.7.jar

    需要 rhino-1.7.7.jar 包 代码示例: package cn.java.mozilla.javascript; import org.mozilla.javascript.Context ...

  2. 怎样查看web软件例如apache的连接数

    查看连接总数和当前的连接数 netstat -ant | grep $ip:80 | wc -l netstat -ant | grep $ip:80 | grep EST | wc -l 查看IP访 ...

  3. WIFI共享大师无法开启发射功能

    1.打开服务(ctrl+R)输入services.msc 2.将关于wifi的服务打开 这里有windows移动热点服务和WLAN开头的服务

  4. SQLite3 of python

    SQLite3 of python 一.SQLite3 数据库 SQLite3 可使用 sqlite3 模块与 Python 进行集成,一般 python 2.5 以上版本默认自带了sqlite3模块 ...

  5. U1

    如果 activity_main.xml没有xml代码可以对图像右键 go to mxl同时可以在design  和  text  切换 在安装了Android Studio3.3版本之后,第一个He ...

  6. DOS中断及程序调用

    http://www.cnblogs.com/ynwlgh/archive/2011/12/12/2285017.html

  7. Debug调试文件

    在debug.h中设置g_debug_switch即可控制调试级别. /* debug.c */ #include "debug.h" const char *get_log_le ...

  8. 使用Spark Streaming + Kudu + Impala构建一个预测引擎

    随着用户使用天数的增加,不管你的业务是扩大还是缩减了,为什么你的大数据中心架构保持线性增长的趋势?很明显需要一个稳定的基本架构来保障你的业务线.当你的客户处在休眠期,或者你的业务处在淡季,你增加的计算 ...

  9. Git-起步

    Git命令行 只要输入git,Git就会不带任何参数地列出它的选项和最常用的子命令. 要得到一个完整的git子命令列表,可以输入git help --all 显示版本号 git --version 每 ...

  10. eclipse中设置JVM内存

    一.   修改jdk 使用内存: 找到eclispe 中window->preferences->Java->Installed JRE ,点击右侧的Edit 按钮,在编辑界面中的 ...