LeetCodeOJ. String to Integer (atoi)
试题请參见: https://oj.leetcode.com/problems/string-to-integer-atoi/
题目概述
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.
spoilers alert...
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 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.
解题思路
这道题目还真是艰辛~ 看起来非常基础, 但是有些Case还真是暗藏玄机.
源码
class Solution {
public:
int atoi(const char *str) {
int n = 0;
bool isPositive = true;
size_t i = 0;
// Ignore Spaces
for ( ; str[i] == ' ' && str[i] != 0; ++ i ) { }
// Process Sign Bit
if ( str[i] == '+' || str[i] == '-' ) {
isPositive = (str[i] == '+');
++ i;
}
// Convert to Integer
for ( ; isDigit(str[i]) && str[i] != 0; ++ i ) {
char digit = str[i] - '0';
int previousResult = n;
n = n * 10 + digit;
// If it's Overflow
if ( n / 10 != previousResult ) {
if ( isPositive ) {
return INT_MAX;
} else {
return INT_MIN;
}
}
}
return ( isPositive ? n : -n );
}
private:
bool isDigit(char digit) {
return (digit >= '0' && digit <= '9');
}
};
LeetCodeOJ. String to Integer (atoi)的更多相关文章
- 【leetcode】String to Integer (atoi)
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- No.008 String to Integer (atoi)
8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...
- leetcode第八题 String to Integer (atoi) (java)
String to Integer (atoi) time=272ms accepted 需考虑各种可能出现的情况 public class Solution { public int atoi( ...
- 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 ...
- String to Integer (atoi) - 字符串转为整形,atoi 函数(Java )
String to Integer (atoi) Implement atoi to convert a string to an integer. [函数说明]atoi() 函数会扫描 str 字符 ...
- Kotlin实现LeetCode算法题之String to Integer (atoi)
题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...
- LeetCode--No.008 String to Integer (atoi)
8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...
- leetcode-algorithms-8 String to Integer (atoi)
leetcode-algorithms-8 String to Integer (atoi) Implement atoi which converts a string to an integer. ...
- LeetCode: String to Integer (atoi) 解题报告
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
随机推荐
- UIMenuController在label中的使用
要想在label中使用 必须是继承于label的分类 //// MYlabel.m// MenuController//// Created by 张明 on 16/3/8.// Copyri ...
- 最新VMware Workstation 10注册码,绝对可用!
最近公司要在solaris上测试产品,需要用到虚拟机,于是下载了最新的虚拟机VMware Workstation 10,并找到了破解码,与大家共享: VMware workstation 10破解序列 ...
- HDU 1796 容斥原理 How many integers can you find
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1796 处男容斥原理 纪念一下 TMD看了好久才明白DFS... 先贴代码后解释 #includ ...
- 面向对象之静态方法(static)和实例化方法的区别
这是一个经常被时时提出来的问题,很多时候我们以为理解了.懂了,但深究一下,我们却发现并不懂. 方法是我们每天都在写得,很多程序员大多都使用实例化方法,而很少使用静态方法,问原因也说不出来所以然,或者简 ...
- UML初了解
最近项目中有看UML图,很是模糊一些东西,今天查资料,发现这篇博文很好,转载了,也解了不少疑惑. 原文:http://www.uml.org.cn/oobject/201008311.asp 在绘画U ...
- 2013 Changsha Regional 一样的木板一样的气球
气球: 木板: 比赛现场: 场外: 曲线.... 除了第一题出的太慢了,总体比成都好了很多.... 自此,今年两铜收场,再接再厉,明年要有更好的成绩.
- linux下检测端口是否连通
检测tcp端口使用telnet命令 telnet 例:telnet 192.168.0.1 80 检测udp端口使用uc命令 uc -zu 例:uc -zu 192.169.0.1 80 以上命令 ...
- Zookper
http://blog.csdn.net/yangbutao/article/details/11669609 http://www.oschina.net/code/snippet_255033_2 ...
- Spreadsheets
很水的一道题,提醒自己要认真,做的头都快晕了.考虑26的特殊情况. D - Spreadsheets Time Limit:10000MS Memory Limit:65536KB 6 ...
- AVOS Cloud Hackathon - SegmentFault
AVOS Cloud Hackathon - SegmentFault AVOS Cloud Hackathon