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 ...
随机推荐
- 01.VMware虚拟机上网络连接(network type)的三种模式--bridged、host-only、NAT
VMWare提供了三种工作模式,它们是bridged(桥接模式).NAT(网络地址转换模式)和host-only(主机模式).要想在网络管理和维护中合理应用它们,你就应该先了解一下这三种工作模式. 1 ...
- OV7670配置和调试小结
先上一下OV7670的框架图 OV7670常用寄存器设置说明 直接看OV7670 Implementation Guide (V1.0)等 资料我已经上传了 https://files.cnblogs ...
- MySql Delete不走索引问题
如果delete语句带有查询,写法不对会导致不走索引. 简单粗暴的办法:拆两条sql,一条查询,一条delete ======================= [不走索引的写法] DELETE FR ...
- VSFTP 配置虚拟用户
虚拟用户的特点是只能访问服务器为其提供的FTP服务,而不能访问系统的其它资源.所以,如果想让用户对FTP服务器站内具有写权限,但又不允许访问系统其它资源,可以使用虚拟用户来提高系统的安全性. 在VSF ...
- 使用PHP的反射Reflection获取对象信息
PHP5添加了一项新的功能:Reflection.这个功能使得程序员可以reverse-engineer class, interface,function,method and extension. ...
- elasticsearch-ik
因lucene默认采用英文且英文通过空格就可以断句.而中文则是词组,如果不加载中文词库或插件则会变为一个一个字而非词组,因此需要加载中文词库. 不加分词库所看到的中文分词效果. post _analy ...
- python流程控制for循环
流程控制 for循环 #首先我们用一例子看下用while循环取出列表中值的方法 l=['a','b','c'] i=0 while i<len(l): print(l[i]) i+=1 #whi ...
- ref与out
注意点: ref和out都是按地址传递,使用后都将改变原来参数的数值 方法定义和调用方法都必须显式使用 ref/out 关键字 ref: 作为ref参数传递的变量在方法调用中传递之前必须初始化 out ...
- VBS处理AD帐号密码到期提醒的脚本[zt]
原文:https://gallery.technet.microsoft.com/scriptcenter/f7f5f7ed-14ee-4d0e-81c2-7d95ce7e08f5 '======== ...
- 19.JDBC和数据库访问.md
1.基本功能: Java通过JDBC完成: 2.基本类型,通常用最后一种 3.JDBC简介 Java连接SQL例子: 参考:http://blog.chinaunix.net/uid-20726500 ...