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 ...
随机推荐
- Shell 编程(循环)
for in 循环语句 #!/bin/bash for x in one two three four do echo number $x done 例:取出passwd中每一行name 并输出 he ...
- EMQ笔记
飞行窗口(Inflight Window)保存当前正在发送未确认的Qos1/2消息.窗口值越大,吞吐越高:窗口值越小,消息顺序越严格. 当客户端离线或者飞行窗口(Inflight Window)满时, ...
- 尚硅谷springboot学习1-简介
以前看过springboot的相关知识,当时偷懒没有做笔记,现在忘得已经差不多了,现在趁着过年有时间,再学习一遍,并做下笔记以备忘. 特性 Spring Boot来简化Spring应用开发,约定大于配 ...
- libcurl+OpenSSL 库分享
首先,我要感谢这两个博客给我的帮助: https://www.cnblogs.com/findumars/p/7496122.html https://blog.csdn.net/yannanxiu/ ...
- Error building Player: Exception: Could not start java
4.6.1发布Flash版本出错.解决方法:把C:\Windows\System32\java.exe复制到C:\Windows\SysWOW64下即可
- 浅谈MySQL事务及隔离级别
目录 1.什么是事务 2.事务的ACID属性 2-1.原子性(Atomicity) 2-2.一致性(Consistency) 2-3.隔离性(Isolation) 2-4.持久性(Durability ...
- ajaxfileupload.js上传文件兼容IE7及以上版本
要兼容IE789,要修改ajaxfileupload.js;要将此处的代码替换掉 if(window.ActiveXObject) { var io = document.createElement( ...
- 19.struts-配置详解.md
目录 1.package name extends abstract namespace 2.action name class method 3.result name type 标签体 <p ...
- 26.如何使用python操作我们自己创建的docker image呢?
因为逻辑复杂 我们建个文件来 python #是单行注释 '''是多行注释 或者””” 我们想使用python来操作docker 那么就要一个api https://github.com/docker ...
- arcgis_SDE安装步骤
弄了将近一个星期的Oracle和ArcSDE终于让我给弄好了!下面把过程跟大家分享一下: 首先是Oracle10gR2的安装,在Oracle的官方网站上可以下到Oracle10gR2的安装程序,安装过 ...