008 String to Integer (atoi) 字符串转换为整数
详见:https://leetcode.com/problems/string-to-integer-atoi/description/
实现语言:Java
class Solution {
public int myAtoi(String str) {
int index =0;
Long total = new Long(0);
int sign = 1;
while(index < str.length() && str.charAt(index) == ' '){
++index;
}
if(index == str.length()){
return (int)(total*sign);
}
if(str.charAt(index) == '-' || str.charAt(index) == '+'){
sign = str.charAt(index) == '-'?-1 : 1;
index++;
}
while(index < str.length() && str.charAt(index) >= '0' && str.charAt(index) <= '9'){
total = total *10 + str.charAt(index) - '0';
++index;
if(total > Integer.MAX_VALUE){
return sign==1?Integer.MAX_VALUE:Integer.MIN_VALUE;
}
}
return (int)(total*sign);
}
}
实现语言:C++
class Solution {
public:
int myAtoi(string str) {
int n=str.size();
if(n==0||str.empty())
{
return 0;
}
int sign=1,base=0,i=0;
while(i<n&&str[i]==' ')
{
++i;
}
if(str[i]=='+'||str[i]=='-')
{
sign=str[i++]=='+'?1:-1;
}
while(i<n&&str[i]>='0'&&str[i]<='9')
{
if(base>INT_MAX/10||(base==INT_MAX/10&&str[i]-'0'>7))
return sign==1?INT_MAX:INT_MIN;
base=base*10+str[i++]-'0';
}
return sign*base;
}
};
参考:http://www.cnblogs.com/grandyang/p/4125537.html
008 String to Integer (atoi) 字符串转换为整数的更多相关文章
- [LeetCode] String to Integer (atoi) 字符串转为整数
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- [leetcode]8. String to Integer (atoi)字符串转整数
Implement atoi which converts a string to an integer. The function first discards as many whitespace ...
- [LeetCode] 8. String to Integer (atoi) 字符串转为整数
Implement atoi which converts a string to an integer. The function first discards as many whitespace ...
- 【LeetCode】8. String to Integer (atoi) 字符串转换整数
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字符串转整数,atoi,题解,Leetcode, 力扣,P ...
- Leetcode8.String to Integer (atoi)字符串转整数(atoi)
实现 atoi,将字符串转为整数. 该函数首先根据需要丢弃任意多的空格字符,直到找到第一个非空格字符为止.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连续的数字组合起来,这部分字 ...
- 【LeetCode】8. String to Integer (atoi) 字符串转整数
题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...
- 【LeetCode】String to Integer (atoi)(字符串转换整数 (atoi))
这道题是LeetCode里的第8道题. 题目要求: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们 ...
- No.008 String to Integer (atoi)
8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...
- String to Integer (atoi) - 字符串转为整形,atoi 函数(Java )
String to Integer (atoi) Implement atoi to convert a string to an integer. [函数说明]atoi() 函数会扫描 str 字符 ...
随机推荐
- BZOJ1033:[ZJOI2008]杀蚂蚁
我对模拟的理解:https://www.cnblogs.com/AKMer/p/9064018.html 题目传送门:https://www.lydsy.com/JudgeOnline/problem ...
- 关于Snoop的用法
snoop是开发wpf应用程序的利器.用它可以观察WPF的可视树,监听事件,更改元素属性等. 下面我介绍下snoop一些用法. 1.获取指定应用程序的UI 打开snoop,选择"Drag ...
- Python:.join()函数
转于:https://blog.csdn.net/chixujohnny/article/details/53301995 博主:chixujohnny 介绍:.join是一个字符串操作函数,将元素相 ...
- 使用py 和flask 实现的服务器系统目录浏览,日志文件实时显示到网页的功能
看日志希望带有彩色,希望从浏览器上看到,不用连到机器上看. 浏览系统的文件夹,scan + 系统文件夹的层级名字当做url路由,可以深层次看到机器上任何层级的文件夹,实现系统文件夹浏览下载. 如果是点 ...
- eclipse个人觉得有用的快捷键
CTRL+SHIFT+F 自动整理代码格式 CTRL+M 最大/还原当前编辑窗口 CTRL+/ 注释当前行 CTRL+1 快速修复 CTRL+D 删除当前行 SHIFT+ENTER 在当前行前面插入空 ...
- ubuntu server usb安装盘制作问题
本来服务器上装的是windows server 2003,开多个虚拟机装linux来用,但发现不管是vmware还是virtualbox,总是有内存泄漏,大约2个星期左右16G内存就全没了,任务管理器 ...
- 10、Perl5中19个最重要的文件系统工具
转载:http://www.cnblogs.com/nkwy2012/p/6027157.html 在写脚本处理文件系统时,经常需要加载很多模块.其中好多有用函数分散在各种不同的模块中.它们有些是Pe ...
- 13、ubuntu终端快捷键(参考 dy9776)
1.终端的快捷键 Tab 自动补全 Ctrl+a 光标移动到开始位置 Ctrl+e 光标移动到最末尾 Ctrl+l 相当于clear,即清屏 Ctrl+Z 把当前任务放到后台运行(相当于运行命令时后面 ...
- 2. DVWA亲测命令执行漏洞
先看low级: 提示让我们输入一个IP地址来实现ping,猜测会是在系统终端中实现的, 我们正常输入127.0.0.1: 那我们就可以利用这个使用其他CMD命令 我们输入127.0.0.1& ...
- 20169201 使用Metaspoit攻击MS08-067实验
MS08-067漏洞介绍 MS08-067漏洞的全称为"Windows Server服务RPC请求缓冲区溢出漏洞",如果用户在受影响的系统上收到特制的 RPC 请求,则该漏洞可能允 ...