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 ...
随机推荐
- HTTP基本协议(查看网页代码)
此示例已实现查看网页的代码来理解HTTP基本协议: (返回的是百度首页的网页代码) import java.io.BufferedReader; import java.io.IOException; ...
- readv和writev函数
readv 和 writev 函数用于在一次函数调用中读.写多个非连续缓冲区.有时也将这两个函数称为散布读和聚集写. #include <sys/uio.h> ssize_t readv( ...
- Codeforces Round #261 (Div. 2)——Pashmak and Buses
题目链接 题意: n个人,k个车,d天.每一个人每天能够坐随意一个车.输出一种情况保证:不存在两个人,每天都在同一辆车上 (1 ≤ n, d ≤ 1000; 1 ≤ k ≤ 109). 分析: 比赛中 ...
- C#语言基础之转义字符、变量、常量、类型转换
1.转义字符: Tab键:/t 反斜杠:// 单引号:/’ 双引号:/” 回车:/r 换行:/n 警告:/a 退格:/b 换页:/f 空:/0 2.变量 ...
- MySQL支持emoji
方案1: 应用层支持 MySQL默认的数据库编码是utf8,对于emoji文字是不能直接存储的,要想存储emoji,有许多库支持对emoji的转换,例如将
- html 7大知识点
HTML是web前端开发的基础,学习前端的人都是先从html学起的. 关于HTML有一些必备的知识点,这些知识点都是HTML中最基本的内容,也是前端面试最常问的知识点. 1.网页结构网页结构一般都包含 ...
- STL之set和multiset(集合)
set和multiset会根据特定的排序准则,自动将元素进行排序.不同的是后者允许元素重复而前者不允许. constructing sets #include #include using names ...
- BZOJ 1880: [Sdoi2009]Elaxia的路线( 最短路 + dp )
找出同时在他们最短路上的边(dijkstra + dfs), 组成新图, 新图DAG的最长路就是答案...因为两人走同一条路但是不同方向也可以, 所以要把一种一个的s,t换一下再更新一次答案 ---- ...
- crontab中使用mysql问题
第1列分钟1-59第2列小时1-23(0表示子夜)第3列日1-31第4列月1-12第5列星期0-6(0表示星期天)第6列要运行的命令 , 并列 - 连续 crontab中不能执行mysql,百分之 ...
- IOS 隐藏键盘。
在View的UITextField中经常需要输入完文字后隐藏软键盘,要实现着一点要让View的Controller实现UITextFieldDelegate代理,然后编写相应的代码. #import ...