【LeetCode】008. 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.
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.
题解:
1. 跳过开始的所有空格
2.如果有 '+'、‘-’ ,则存储正负号。
3.如果不是数字,则直接返回 0
4.在 3 过程中,溢出条件(前提是 res 还未添加当前遍历的数字 digit):
1) res > INT_MAX
2)sign == 1 && res == INT_MAX && digit > 7
3)sign == -1 && res == INT_MIN && digit > 8
- class Solution {
- public:
- int myAtoi(string str) {
- int res = ;
- int n = str.size();
- int i = ;
- int sign = ;
- while (i < n && str[i] == ' ') ++i;
- if (i == n)
- return ;
- if (str[i] == '+' || str[i] == '-')
- sign = (str[i++] == '-') ? - : ;
- while (i < n && str[i] >= '' && str[i] <= '') {
- int digit = str[i++] - '';
- if (res > INT_MAX / || res == INT_MAX / && digit > )
- return sign == ? INT_MAX : INT_MIN;
- res = res * + digit;
- }
- return res * sign;
- }
- };
【LeetCode】008. String to Integer (atoi)的更多相关文章
- 【LeetCode】8. String to Integer (atoi) 字符串转换整数
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字符串转整数,atoi,题解,Leetcode, 力扣,P ...
- 【LeetCode】8. String to Integer (atoi) 字符串转整数
题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...
- 【leetcode】8. String to Integer (atoi)
题目描述: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ...
- 【一天一道LeetCode】#8. String to Integer (atoi)
一天一道LeetCode系列 (一)题目 Implement atoi to convert a string to an integer. Hint: Carefully consider all ...
- 《LeetBook》leetcode题解(8): String to Integer (atoi) [E]——正负号处理
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 【LeetCode】字符串 string(共112题)
[3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...
- No.008 String to Integer (atoi)
8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...
- LeetCode--No.008 String to Integer (atoi)
8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...
- 【LeetCode】7 & 8 - Reverse Integer & String to Integer (atoi)
7 - Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Notic ...
随机推荐
- Kattis - abc 【水】
题意 给出三个数,然后给出一个顺序,有ABC三个字母构成, A是最大的数字 B是中间的数字 C是最小的数字 根据 ABC的顺序 给出 数字的顺序 思路 先排序一下,然后用 MAP 双向标记一下 AC代 ...
- deeplink
http://www.cnblogs.com/shadajin/p/5724117.html Deeplink,简单讲,就是你在手机上点击一个链接之后,可以直接链接到app内部的某个页面,而不是app ...
- VSCode隐藏node_modules目录
使用VSCode,打开一个工程时,发现node_modules目录包含到工程中了,问题: settings.json配置如下,可以自己定制忽略的文件夹: search.exclude 用来忽略搜索的文 ...
- 建议47:使用logging记录日志信息
# -*- coding:utf-8 -*- ''' Python中自带的logging 模块提供了日志功能,它将logger 的level 分为5 个级别 DEBUG 详细的信息,在追踪问题的时候使 ...
- Redis之数据存储结构
今天去中关村软件园面试,被问到:你做项目用到的Redis处理数据用的什么结构?顿时石化,”用到的结构,不就是key-value嘛,还有什么结构?“.面试官说:“平时除了工作,要加强学习,下面的面试我觉 ...
- CSS3展开带弹性动画的手风琴菜单
在线演示 本地下载
- jQuery之DOM操作大全
jQuery属性操作 获取元素属性的语法:attr(name) 例子:$("#img1").attr("src"); 设置元素单个属性的语法:attr(key, ...
- poj 3468 线段树模板题
#include<iostream> #include<algorithm> #include<stdio.h> using namespace std; #def ...
- /var/spool/clientmqueue 爆满问题
当你使用简单的sendmail发邮件的时候,或者系统默认要发一些邮件(比如cron发的邮件)的时候,首先会把邮件拷贝到这个目录里,然后等待MTA(mail transfer agent) 来处理,MT ...
- JavaScript实现的功能
1.js事件阻止冒泡的应用 1)问题描述: 单机除了这两个元素,触发事件,: 可用阻止事件冒泡 2)解决方法: $('body').click(function(e){ $('#searchTree' ...