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 字符 ...
随机推荐
- 简单的RBAC用户角色权限控制
Java web项目中,无论项目是大是小,或多或少都会涉及到用户访问权限的控制,权限管理总体的设计思路就是,不该看的不看,不该做的不做!据我目前的了解,我所知道的几种实现访问权限控制的方式有: JQu ...
- UILabel常见用法
//创建一个UILabel UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(50 , 100 , 200 , 560)]; // ...
- CentOS 7关闭firewalld启用iptables
在CentOS7中,有很多CentOS 6中的常用服务发生了变化. 其中iptables是其中比较大的一个.防火墙iptables被firewalld取代. 本文将介绍,如果采用systemctl关闭 ...
- Qt安装与配置
安装Qt 安装Qt Creator,打开终端执行如下命令: sudo apt-get install qt5-default qtcreator -y 安装Qt示例和文档: sudo apt-get ...
- HDU1698(线段树入门题)
Just a Hook Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Descrip ...
- Date---String is 合法的date 方法---
package com.etc.jichu; import java.text.SimpleDateFormat; public class IsDate { public static boolea ...
- Python-实现与metasploit交互并进行ms17_010攻击
关于ms17_010,可参考http://www.cnblogs.com/sch01ar/p/7672454.html 目标IP:192.168.220.139 本机IP:192.168.220.14 ...
- 使用mahout fpgrowth算法求关联规则
使用mahout fpgrowth 首先,这篇文章的内容大部分取自国外一篇博客Finding association rules with Mahout Frequent Pattern Mining ...
- 关于C/C++中的“auto”关键字
C/C++ 98标准 C++03标准 早在C++98标准中就存在了auto关键字,那时的auto用于声明变量为自动变量,自动变量意为拥有自动的生命期.此用法是多余的,因为即使定义变量时不加" ...
- rest framework 节流
一.简单节流示例 所谓节流就是控制用户访问频率,这里分为匿名用户(非登录用户)和登录用户的限制. 匿名用户:根据其 IP 限制其频率 登录用户:IP.用户名都 OK 获取用户请求 IP:request ...