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 ...
随机推荐
- 概率图模型(PGM)学习笔记(三)模式判断与概率图流
我们依旧使用"学生网络"作为样例,如图1. 图1 首先给出因果判断(Causal Reasoning)的直觉解释. 能够算出来 即学生获得好的推荐信的概率大约是0.5. 但假设我们 ...
- QQ聊天原理初识
1:qq之间文件的传输是通过p2p通信进行的. 2:qq之间的表情发送实际上就是文字的发送,是client再接受到文字之后在本地自己进行转换 3:qq之间的通信既能够通过udp也能够通过Tcp 尽管u ...
- struts2自己定义类型转换器
1.1. struts2自己定义类型转换器 1) 自定类型转换类,继承DefaultTypeConverter类 package com.morris.ticket.conversio ...
- plsql developer连接64位Oracle11g的解决方法
1)安装Oracle 11g 64位 2)安装32位的Oracle客户端( instantclient-basic-win32-11.2.0.1.0)下载地址:http://www.oracle.co ...
- 行列转换小结 Pivot ,Unpivot (转,改)
行专列 Pivot 1)SQL 2000版本 静态 SELECT ID , SUM(CASE Code WHEN 'Item1' THEN Value END) AS Item1 , SUM(CASE ...
- 通过OC实现简单的冒泡排序
NSMutableArray *arr = [@["] mutableCopy]; ; i<[arr count]-; i++) { ; j<[arr count]--i; j+ ...
- JavaSE学习总结第05天_Java语言基础1
05.01 方法概述和格式说明 简单的说:方法就是完成特定功能的代码块 在很多语言里面都有函数的定义,函数在Java中被称为方法 格式: 修饰符返回值类型方法名(参数类型参数名1,参数类型参数名2 ...
- dialog组件的jquery封装实现
(function($){ $.extend({ Dialog : function(id, options){ var option = $.extend({}, options); option. ...
- mysql root密码重置
1.修改my.cnf #位置一般是 /etc/my.cnf 2.重启mysql服务 service mysqld restart 3.进入mysql mysql -uroot -p 然后直接回车 4. ...
- jQuery(二)
table 全选.反选.清除 <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...