8.String to Integer (atoi) (INT; Overflow)
Implement atoi to convert a string to an integer.
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.
思路:INT最大是2147483647,最小是-2147483648。所以要尤其注意-2147483648取相反数是得不到2147483648的。
class Solution {
public:
int myAtoi(string str) {
int ret = ;
int i = ;
bool pos = true;
while(str[i] == ' ') i++; //neglect the spaces at the start
if(str[i] == '+') i++;
else if(str[i] == '-') {
pos = false;
i++;
}
while(str[i] != '\0'){
if(str[i] > '' || str[i] <'') break; //if invalid digit, stop scanning and return the current result
if(ret > || (ret == && str[i] - '' > )){ //if overflow, return the extreme value
if(pos) return INT_MAX;
else return INT_MIN;
}
ret = ret * + str[i] - '';
i++;
}
if(pos) return ret;
else return (-ret);
}
};
8.String to Integer (atoi) (INT; Overflow)的更多相关文章
- 【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 ...
随机推荐
- supervisord管理进程详解
supervisord管理进程详解 supervisor配置详解(转) 官网 Linux后台进程管理利器:supervisor supervisor使用详解
- Web安全测试指南--会话管理
会话复杂度: 5.3.2.会话预测: 5.3.3.会话定置: 5.3.4.CSRF: 5.3.5.会话注销: 5.3.6.会话超时:
- VS2013下.Net Framework4配置FineUI4.14
配置步骤: 工具箱:空白处右键--选项卡--浏览,选择FineUI.dll配置web.config,管道模式设置为:传统 配置web.config 在form表单下添加: <f:PageMana ...
- WCF服务部署
一.将WCF服务部署到IIS上 1.首先检测电脑上是否安装了IIS,一般来说Win7以上系统自带IIS 2.下面进行IIS服务的开启设置 控制面板=>打开或关闭Windos功能 3.勾选该窗口中 ...
- Linux内存管理大图(第三稿)
http://bbs.chinaunix.net/thread-2018659-2-1.html 描述讨论在http://bbs.chinaunix.net/thread-3760371-1-1.ht ...
- gvim下用Vundle安装solarized主题的方法
1.在.vimrc中加入 Bundle 'Solarized' 2.重启gvim,并执行 :BundleInstall 3.将solarized.vim文件放入.vim下的colors文件夹内(如果没 ...
- JAVA 连接 Redis 并进行操作
1, 这里以maven项目为例 <!-- Redis NoSQL 操作依赖 --> <dependency> <groupId>redis.clients</ ...
- C# EF 基础操作
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- apache中 MaxClients 与MaxRequestsPerChild
据现象来对APACHE调优,以前用MAXCLIENTS 3000,砖家建议后,改为1500,今天查资料如下: http://www.linuxqq.net/ MaxClients 要加到多少?连接数理 ...
- 机器学习入门-贝叶斯中文新闻分类任务 1. .map(做标签数字替换) 2.CountVectorizer(词频向量映射) 3.TfidfVectorizer(TFDIF向量映射) 4.MultinomialNB()贝叶斯模型构建
1.map做一个标签的数字替换 2.vec = CountVectorizer(lowercase=False, max_features=4000) # 从sklean.extract_featu ...