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. DDOS与DOS的区别

    0x01 在说之前,先看一些例子吧 还有众所周知的中美黑客大战,新闻我就不说了 0x02 什么是DOS DoS是Denial of Service的简称,即拒绝服务,造成DoS的攻击行为被称为DoS攻 ...

  2. 【PHP】什么时候使用Try Catch(转)

    几条建议: 如果无法处理某个异常,那就不要捕获它.  如果捕获了一个异常,请不要胡乱处理它.  尽量在靠近异常被抛出的地方捕获异常.  在捕获异常的地方将它记录到日志中,除非您打算将它重新抛出.  按 ...

  3. kafka及扩展的安装笔记

    参考文件 https://blog.csdn.net/weiwenjuan0923/article/details/76152744 一.首先确认下jdk有没有安装 安装参照这个连接 https:// ...

  4. 用python给图片添加半透明水印

    # coding:utf-8 from PIL import Image, ImageDraw, ImageFont def add_text_to_image(image, text): font ...

  5. 适合学习的QT开源项目-SerialTool

    https://github.com/Skiars/SerialTool A cross platform Serial-Port/TCP/UDP debugging tool. SerialTool ...

  6. Linux5个数据段

        进程(执行的程序)会占用一定数量的内存,它或是用来存放从磁盘载入的程序代码,或是存放取自用户输入的数据等等.不过进程对这些内存的管理方式因内存用途不一而不尽相同,有些内存是事先静态分配和统一回 ...

  7. HDU - 1054 Strategic Game (二分图匹配模板题)

    二分图匹配模板题 #include <bits/stdc++.h> #define FOPI freopen("in.txt", "r", stdi ...

  8. Java面向对象---方法的创建与重载

    方法的创建 方法就是可重复调用的代码段. 定义: 访问修饰符 返回值类型 方法名(参数){ 方法主体 } 返回值类型:void(无返回值):基本数据类型:应用数据类型:类对象等. 方法名的命名规则:第 ...

  9. 【N-Queens】cpp

    题目: The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two que ...

  10. Android SDK 目录详解(转)

    Android SDK目录结构和工具介绍是本文要介绍的内容,主要是来了解并学习Android SDK的内容,具体关于Android SDK内容的详解来看本文. Android SDK目录下有很多文件夹 ...