leetcode-algorithms-8 String to Integer (atoi)
leetcode-algorithms-8 String to Integer (atoi)
Implement atoi which converts a string to an integer.
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.
Note:
- Only the space character ' ' is considered as whitespace character.
- Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−2^31, 2^(31 − 1)]. If the numerical value is out of the range of representable values, INT_MAX (2^31 − 1) or INT_MIN (−2^31) is returned.
Example 1:
Input: "42"
Output: 42
Example 2:
Input: " -42"
Output: -42
Explanation: The first non-whitespace character is '-', which is the minus sign.
Then take as many numerical digits as possible, which gets 42.
Example 3:
Input: "4193 with words"
Output: 4193
Explanation: Conversion stops at digit '3' as the next character is not a numerical digit.
Example 4:
Input: "words and 987"
Output: 0
Explanation: The first non-whitespace character is 'w', which is not a numerical
digit or a +/- sign. Therefore no valid conversion could be performed.
Example 5:
Input: "-91283472332"
Output: -2147483648
Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer.
Thefore INT_MIN (−231) is returned.
解法:
class Solution {
public:
int myAtoi(string str)
{
long long convert = 0;
int indicator = 1;
int i = str.find_first_not_of(' ');
if(str[i] == '-' || str[i] == '+')
indicator = (str[i++] == '-') ? -1 : 1;
while('0'<= str[i] && str[i] <= '9')
{
convert = convert * 10 + (str[i++] - '0');
if(convert * indicator >= INT_MAX) return INT_MAX;
if(convert * indicator <= INT_MIN) return INT_MIN;
}
return convert * indicator;
}
};
leetcode-algorithms-8 String to Integer (atoi)的更多相关文章
- 《LeetBook》leetcode题解(8): String to Integer (atoi) [E]——正负号处理
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 【一天一道LeetCode】#8. String to Integer (atoi)
一天一道LeetCode系列 (一)题目 Implement atoi to convert a string to an integer. Hint: Carefully consider all ...
- 【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】008. 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)
又是一道恶心的简单题. 一开始没想到这么多情况的,幸好LeetCode是个很人性化的oj,能让你知道你在哪个case上错了,否则一辈子都过不了. 考虑不周到只能一个个补了. 列举一下恶心的case / ...
- LeetCode(8) - String to Integer (atoi)
虽然是easy,却是比较繁琐的一道题,需要考虑各种边界条件.在WA了好几遍之后,才把各种边界条件给补全.需要考虑到的因素如下: 输入不合法字符,非"0-9",对于首位,合法字符还包 ...
- 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 ...
- Kotlin实现LeetCode算法题之String to Integer (atoi)
题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...
随机推荐
- 论文笔记:Learning how to Active Learn: A Deep Reinforcement Learning Approach
Learning how to Active Learn: A Deep Reinforcement Learning Approach 2018-03-11 12:56:04 1. Introduc ...
- Luncene学习二《搜索索引》
搜索索引的流程 第一步:创建一个Directory对象,也就是索引库存放的位置 第二步:创建一个IndexReader对象,需要指定Directory对象 第三步:创建一个indexsearcher对 ...
- Linux中.rar文件解压
1. 下载: https://www.rarlab.com/download.htm 我下载的是RAR 5.61 for Linux x64 2. 安装: 解压:tar -zxvf rarlinux- ...
- HDU 5575 Discover Water Tank(左偏树)
https://vjudge.net/problem/HDU-5575 题意: 有一个水箱,被n-1块板子分成了n个部分,板子的高度不尽相同.现在有m次探测,每次探测在第x部分的y+0.5高度处是否有 ...
- 解决:springmvc中接收date数据问题
这里提供三种解决方案. 一.局部转换 :只是对当前Controller类有效 springMVC.xml中添加: <bean class="org.springframework.we ...
- 【四】php 函数
一:函数 作用:为了分割那些能够独立完成有明确任务的代码,更易于阅读 调用函数:function_name(args1,args2...); args可以是任何一种php变量,包括数组或对象 函数名不 ...
- 函数indexOf()和lastIndexOf()
返回前面起第一个字符的位置indexOf(“字符”); 它是从前面开始数(从左边开始数),而且只找第一个,然后返回该字符的位置,索引号都是从0开始的.返回的是个数值. var txt = “abcde ...
- java+selenium的helloworld
在学校上测试课程,接触到自动化管理工具,在加上助教工作需要改作业,所以想着学下selenium这一强大的web自动化工具. 1.lenium官网:http://www.seleniumhq.org/ ...
- 整数m去掉n位后剩下最大(小)值
题目描述 给定一个正整数(<=255位),从中删去n位后,使得剩下的数字组成的新数最小(大): 思路:从左到右开始扫描,两两比较,如果是前一位比后一位大,则删去前大的一位,直到删完所有的n位: ...
- 大数据新手之路三:安装Kafka
Ubuntu16.04+Kafka1.0.0 1.下载kafka_2.11-1.0.0.tgz http://kafka.apache.org/downloads 2.解压到/usr/local/ka ...