leetcode-8-String to Integer (atoi) (已总结)
8. String to Integer (atoi)
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.
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,本身并不难,但是边界条件很多:
1.允许开头*个空字符,允许开头有'-''+'
2.不允许数字中出现除'0'-'9'以外的字符
3.结果大于MAX小于MIN返回MAX个MIN
class Solution {
public:
int myAtoi(string str) { int ret = , tmp;
int i = , flag = ;
int len = str.length(); // special case : ""
if (len == ) return ; // special case : " (+/-)123"
while (str[i] == ' ') i++;
if (str[i] == '-') {
i++;
flag = -;
}
else if (str[i] == '+')
i++; for (;i < len; i++) {
if (str[i] < '' || str[i] > '') break; tmp = ret * + str[i] - '';
if (tmp / != ret)
if (flag == ) return INT_MAX;
else return INT_MIN; ret = tmp;
} return ret * flag;
}
};
注意:1.可用ASCII码 2.line(25)判断是否越界写的很好
tmp = ret * + str[i] - '';
if (tmp / != ret)
if (flag == ) return INT_MAX;
else return INT_MIN;
leetcode-8-String to Integer (atoi) (已总结)的更多相关文章
- Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)
Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...
- leetcode day6 -- String to Integer (atoi) && Best Time to Buy and Sell Stock I II III
1. String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully con ...
- 【leetcode】String to Integer (atoi)
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- [leetcode] 8. String to Integer (atoi) (Medium)
实现字符串转整形数字 遵循几个规则: 1. 函数首先丢弃尽可能多的空格字符,直到找到第一个非空格字符. 2. 此时取初始加号或减号. 3. 后面跟着尽可能多的数字,并将它们解释为一个数值. 4. 字符 ...
- Leetcode 8. String to Integer (atoi)(模拟题,水)
8. String to Integer (atoi) Medium Implement atoi which converts a string to an integer. The functio ...
- [LeetCode][Python]String to Integer (atoi)
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/string- ...
- 【LeetCode】String to Integer (atoi) 解题报告
这道题在LeetCode OJ上难道属于Easy.可是通过率却比較低,究其原因是须要考虑的情况比較低,非常少有人一遍过吧. [题目] Implement atoi to convert a strin ...
- LeetCode 8. String to Integer (atoi) (字符串到整数)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- [LeetCode] 8. String to Integer (atoi) 字符串转为整数
Implement atoi which converts a string to an integer. The function first discards as many whitespace ...
- LeetCode 7 -- String to Integer (atoi)
Implement atoi to convert a string to an integer. 转换很简单,唯一的难点在于需要开率各种输入情况,例如空字符串,含有空格,字母等等. 另外需在写的时候 ...
随机推荐
- 解决移动端H5海报滑动插件适应大部分手机问题 手机端高度自适应
Html5微信端滑屏海报在各种尺寸的手机上总会有这样那样的问题,经过多次制作总结出来一些小心得,分享下. 我使用的是jquery插件swiper.min.js,动画可以利用animate.css,如果 ...
- jmeter录制https请求时,浏览器每一个请求都 跳 不安全访问页面的解决方法
1.关闭所有浏览器 2,使用终端 输入 : /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --ignore-certif ...
- [html]window.open 使用示例
<!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...
- 第 0 课 Golang环境搭建
1 下载 https://studygolang.com/dl 2 安装完成后,查看环境变量 GOBIN 存放可执行文件的目录的绝对路径. GOPATH 工作区目录的绝对路径.存放远源码的路径 ...
- c#,读取二维码
/// <summary>/// 读取二维码/// 读取失败,返回空字符串/// </summary>/// <param name="filename&quo ...
- java后端时间处理工具类,返回 "XXX 前" 的字符串
转自:https://www.cnblogs.com/devise/p/9974672.html 我们经常会遇到显示 "某个之间之前" 的需求(比如各种社交软件,在回复消息时,显示 ...
- liquibase使用
1. 创建表 drop database if exists mybatis; create database mybatis; use mybatis; create table mybatis.C ...
- VBS 创建快捷方式
Dim Shell,DesktopPath,link Set Shell = CreateObject("WScript.Shell") DesktopPath = Shell.S ...
- 一劳永逸解决VLC播放中文字幕乱码问题
VLC对于Mac/Ubuntu用户来说算得上是必备软件.其相当于PC机上的“暴风影音”,但Mac/Ubuntu的新手使用VLC播放avi时都会碰 到字幕乱码的问题.avi字幕的格式有多种,这里假设你使 ...
- 初步认识session
TestSession01.java protected void doPost(HttpServletRequest request, HttpServletResponse response) t ...