8. String to Integer (整数的溢出)
Implement atoi to convert a string to an integer.
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.
int myAtoi(char* str) {
long int ret = ;
char* p = str;
bool isPos = true;
//get the positive or negative
while(*p == ' '){ //neglect space at the beginning
p++;
continue;
}
if(*p == '+') p++;
else if(*p == '-'){
isPos = false;
p++;
}
//get the digit
while(*p != '\0'){
if(*p < '' || *p > '') break; //invalid character occurs, stop converting
ret = ret* + (*p) - '';
if(ret >= ) break; //out of int range
p++;
}
if(!isPos) ret = -ret;
if(ret > INT_MAX) ret = INT_MAX;
else if(ret < INT_MIN) ret = INT_MIN;
return ret;
}
8. String to Integer (整数的溢出)的更多相关文章
- 7. Reverse Integer (整数的溢出)
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 For the p ...
- LeetCode 8. 字符串转换整数 (atoi)(String to Integer (atoi))
8. 字符串转换整数 (atoi) 8. String to Integer (atoi) 题目描述 LeetCode LeetCode8. String to Integer (atoi)中等 Ja ...
- 剑指offer 67. 字符串转换为整数(Leetcode 8. String to Integer (atoi))
题目:剑指offer 67题 需要考虑的情况:空指针.nullptr.空字符串"".正负号.数值溢出.在写代码的时候对这些特殊的输入都定义好合理的输出.可以定义一个全局布尔型变量g ...
- 【leetcode】String to Integer (atoi)
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- 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 ...
- 《LeetBook》leetcode题解(8): String to Integer (atoi) [E]——正负号处理
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 《LeetBook》leetcode题解(7): Reverse Integer[E]——处理溢出的技巧
我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 书的地址:https://hk029.gitbooks.io/leetboo ...
- [leetcode] 8. String to Integer (atoi) (Medium)
实现字符串转整形数字 遵循几个规则: 1. 函数首先丢弃尽可能多的空格字符,直到找到第一个非空格字符. 2. 此时取初始加号或减号. 3. 后面跟着尽可能多的数字,并将它们解释为一个数值. 4. 字符 ...
- Leetcode 题目整理-2 Reverse Integer && String to Integer
今天的两道题关于基本数据类型的探讨,估计也是要考虑各种情况,要细致学习 7. Reverse Integer Reverse digits of an integer. Example1: x = 1 ...
- Q8:String to Integer (atoi)
8. String to Integer (atoi) 官方的链接:8. String to Integer (atoi) Description : Implement atoi to conver ...
随机推荐
- MySQL(Innodb)索引的原理
引言 回想四年前,我在学习mysql的索引这块的时候,老师在讲索引的时候,是像下面这么说的 索引就像一本书的目录.而当用户通过索引查找数据时,就好比用户通过目录查询某章节的某个知识点.这样就帮助用户有 ...
- Appium -选择、操作元素2
选择元素的方法 根据xpath 在Appium中,我们没法使用css,因为css是web专用的 Appium支持xpath来定位元素 对于一些比较复杂的元素的定位,我们可以用它 driver.find ...
- react-native android打包
看了官网测试的是可以的,自己整理下,方便后面查看 先是生产安卓证书,安卓证书生成,点这里.这里掠过 生成安卓证书,记住2个密码 秘钥库口令 和 私钥密码 1.然后把你生成的安卓证书放到文件放到你工程中 ...
- linux获取线程ID
pthread_self()获取当选线程的ID.这个ID与pthread_create的第一个参数返回的相同.但是与ps命令看到的不同,因此只能用于程序内部,用于对线程进行操作. #include & ...
- HTML 圆心节点
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 我要重新开始学习C++了!
C++实在是博大精深!之前总不想读厚厚的C++ Primer. 然而,现在的水平真的只是初学者!只是因为写的代码太简单,所以没有用到一些特性.可以说还是门外汉! 写笔记!
- CSS中clear属性的both、left和right浅析
前端开发中,我们知道clear属性有none.both.left和right四个值. 它们的具体含义如下: none:允许两边都可以有浮动对象: both:不允许有浮动对象; left:不允许左边有浮 ...
- MySql出现大量LAST_ACK的解决办法
前几日生产环境遇到一问题,网站的同步登录部分提示Can’t connect to MySQL server on ‘localhost’ (10060),第一反应就是可能过连接数据库的相关参数了,经检 ...
- JSON数据的解析和生成(Swift)
Codable public typealias Codable = Decodable & Encodable public protocol Decodable {} public pro ...
- android填满手机内存的方法
1. 进行临界测试,手机盘空间存满的条件下应用会有何表现:通常手动添加大文件但是还是不够,通过如下 2. 使用adb命令完成:通过如下 adb 命令在 /mnt/sdcard/ 目录下产生一个名为 b ...