eetcode 之String to Integer (atoi)(28)
字符串转为数字,细节题。要考虑空格、正负号,当转化的数字超过最大或最小是怎么办。
int atoi(char *str)
{
int len = strlen(str);
int sign = ;
int num = ; int i = ;
while (str[i] == ' '&& i < len)i++; if (str[i] == '+')i++;
else if (str[i] == '-')
{
sign = -;
i++;
} for (; i < len; i++)
{
if (str[i]<''&&str[i]>'')break;
if (num>INT_MAX / || (num==INT_MAX / && (str[i] - '')>INT_MAX % ))
{
return sign == - ? INT_MIN : INT_MAX;
}
num = num * + str[i] - '';
} return num*sign;
}
eetcode 之String to Integer (atoi)(28)的更多相关文章
- 【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 ...
随机推荐
- HDU4825:Xor Sum——题解
http://acm.hdu.edu.cn/showproblem.php?pid=4825 Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含 ...
- [Leetcode] pascals triangle ii 帕斯卡三角
Given an index k, return the k th row of the Pascal's triangle. For example, given k = 3,Return[1,3, ...
- 关于JavaScript的push()函数
push() 方法可向数组的末尾添加一个或多个元素,并返回新的长度.返回值为把指定的值添加到数组后的新长度. 语法:arrayObject.push(newelement1,newelement2,. ...
- sleep方法和wait方法的区别?
sleep 是线程类(Thread)的方法,导致此线程暂停执行指定时间,给执行机会给其他线程,但是监控状态依然保持,到时后会自动恢复.调用sleep 不会释放对象锁.wait 是Object 类的方法 ...
- 用HTML5 File API 实现截图粘贴上传、拖拽上传
<!DOCTYPE html><html><head> <title>test chrome paste image</title> < ...
- Centos下iptables常用命令
安装iptablesyum install iptables-services 重启防火墙使配置文件生效systemctl restart iptables.service 设置iptables防火墙 ...
- POJ 3255 Roadblocks (次短路模板)
Roadblocks http://poj.org/problem?id=3255 Time Limit: 2000MS Memory Limit: 65536K Descriptio ...
- 「模板」 树链剖分 HLD
「模板」 树链剖分 HLD 不懂OOP的OIer乱用OOP出人命了. 谨此纪念人生第一次类套类. 以及第一次OI相关代码打过200行. #include <algorithm> #incl ...
- 【Codeforces441E】Valera and Number [DP]
Valera and Number Time Limit: 20 Sec Memory Limit: 512 MB Description Input Output Sample Input 5 3 ...
- 【BZOJ】4147: [AMPPZ2014]Euclidean Nim
[算法]博弈论+数论 [题意]给定n个石子,两人轮流操作,规则如下: 轮到先手操作时:若石子数<p添加p个石子,否则拿走p的倍数个石子.记为属性p. 轮到后手操作时:若石子数<q添加q个石 ...