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 字符 ...
随机推荐
- 洛谷【P1177】【模板】快速排序
题目传送门:https://www.luogu.org/problemnew/show/P1177 快排是一种对于冒泡排序的优化. 对于区间\([l,r]\),我们选择一个键值\(k\),让比\(k\ ...
- 使用Rancher搭建K8S测试环境
使用Rancher搭建K8S测试环境 http://blog.csdn.net/csdn_duomaomao/article/details/75316926 环境准备(4台主机,Ubuntu16.0 ...
- 使用python对文件夹里面所有代码行数进行统计。
统计目录下所有的代码个数和总行数. # -*- coding: utf-8 -*- # @Author : ydf import json import os from pathlib import ...
- PHP二维数组,根据多个字段来排序
如果是最最常见的二维数组排序, 大多数情况下也只用到二维: 用php内置函数 array_multisort( ) 是最简单的: <?php 假设, $arr 是一个二维数组, $arg1是取 ...
- Spring整合JUnit4测试时,使用注解引入多个配置文件
转自:https://blog.csdn.net/pwh309315228/article/details/62226372 一般情况下: @ContextConfiguration(Location ...
- LAMP 1.4 PHP编译安装问题解决
环境:centos X64 最小化安装 php版本:php-5.4.3 安装前.先安装些软件和库文件 yum install -y gcc gcc-c++ make zlib zlib-devel p ...
- shell入门-awk-2
awk的条件操作符 ///显示第一段有root的行 [root@wangshaojun ~]# awk -F ':' '$1=="root"' 1.txtroot:x:0:0:ro ...
- Linux Screen超简明教程
1.安装Screen 大多数情况下,系统已经安装好了screen.如果没有,可以用下面的命令来安装: CentOS系统中执行:yum install screen Debian/Ubuntu系统执行: ...
- ObservableCollection 分组后排序报错问题
ObservableCollection通过Move方法可以移动顺序,如下: 将ObservableCollection中的一个item置顶: private ObservableCollection ...
- 一款Regular expression在线检测工具
记录下我自己使用的一款正则表达式使用工具 https://regex101.com/ 输入正则表达式后,可以在下面的“TEST STRING”中来测试对应的字符串是否满足该正则表达式 个人觉得非常好用