【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 ...
随机推荐
- 每天一个Linux命令(47)route命令
Linux系统的route命令用于显示和操作内核IP路由表(show / manipulate the IP routing table). (1)用法: 用法: route ...
- php一些常用功能封装
//二分查找 function bin_sch($array, $low, $high, $k) { if ($low <= $high) { $mid = intval(($low + $hi ...
- 【Head First Servlets and JSP】笔记13:session & cookie
session的接口 杀死会话 cookie的性质 cookie的接口 再总结——cookie.session.JSESSIONID的前世今生 简单的定制cookie示例 1.session的接口,配 ...
- 初识python---简介,简单的for,while&if
一编程语言:编程语言是程序员与计算机沟通的介质: 编程语言的分类: 1机器语言:是用二进制代码表示的计算机能直接识别和执行的一种机器指令的集合. 优点:灵活,直接执行和速度快 ...
- windows 服务安装报错
使用windows服务开发的定时任务,在win7上都运行良好,在windows server 2008上运行报错,报错信息如下 错误应用程序名称: GCWindowsService.exe,版本: 1 ...
- Docker 配置代理
最近在k8s上部署helm 老提示无法下载镜像,因为伟大的祖国的长城Firewall....导致k8s根本玩不了..... 第一步:配置系统代理 # vim .bashrc export http_p ...
- gdoi2017爆零记
前言 这次gdoi,用三个词来形容我:爆零+爆零+爆零.本来还希望能在gdoi搞个小新闻(拿个一等什么的). Day0 这次gdoi是在东莞东华中学,坐个动车下午3点多就到了,然后打个滴滴去酒店(本来 ...
- JAVA Properties配置文件的读写
通常我们就会看到一个配置文件,比如:jdbc.properties,它是以“.properties”格式结尾的.在java中,这种文件的内容以键值对<key,value>存储,通常以“=” ...
- Python中MySQL插入数据
sql = 'INSERT INTO course(class_name, credit, properties, teacher_name, college_given, classroom) ' ...
- nova conductor
nova conductor是一个RPC 服务,所有支持的API都在 nova.conductor.rpcapi.ConductorAPI 它是stateless,可以水平扩展. 优点: 安全: 如果 ...