字符串转数字练习--String to Integer (atoi)
Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value. The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function. If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed. If no valid conversion could be performed, a zero value is returned. Note: Only the space character ' ' is considered as whitespace character.
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned.
Example 1: Input: "42"
Output: 42
Example 2: Input: " -42"
Output: -42
Explanation: The first non-whitespace character is '-', which is the minus sign.
Then take as many numerical digits as possible, which gets 42.
Example 3: Input: "4193 with words"
Output: 4193
Explanation: Conversion stops at digit '3' as the next character is not a numerical digit.
Example 4: Input: "words and 987"
Output: 0
Explanation: The first non-whitespace character is 'w', which is not a numerical
digit or a +/- sign. Therefore no valid conversion could be performed.
Example 5: Input: "-91283472332"
Output: -2147483648
Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer.
Thefore INT_MIN (−231) is returned.
Accepted
347,790
Submissions
2,386,606
不知道是不是下午头有点晕的原因,做了那么久
思路:
正则去掉前面的空格
分正负讨论(正的还有可能包括数字前面有+)
string转int可能包越界,先分字符串长度讨论,然后转为long再和int边界比较


class Solution {
public int myAtoi(String str) {
if(str==null||str.length()<1) return 0;
if(str.charAt(0)==' '){
str=str.replaceFirst("^ *", "");
}
if(str.length()<1) return 0;
if(str.length()>1&&str.charAt(0)=='+'&&(str.charAt(1)>'9'||str.charAt(1)<'0')){
return 0;
}
if(str.charAt(0)=='+'){
str=str.substring(1);
}
if(str.length()==1&&((str.charAt(0)>57&&str.charAt(0)<48))) return 0;
String result="";
for(int i=0;i<str.length();i++){
if(i>0){
char c=str.charAt(i);
if(c<=57&&c>=48){
result+=str.substring(i, i+1);
}else{
break;
}
}else{
if(str.charAt(0)=='-'||(str.charAt(0)<=57&&str.charAt(0)>=48)){
result+=str.substring(0, 1);
}else{
return 0;
}
}
}
long l;
if(result!=""){
if(result.charAt(0)=='-'){
result=result.replaceFirst("-", "");
result=result.replaceFirst("^0*", "");
result ="-"+result;
}else{
result=result.replaceFirst("^0*", "");
}
}
if(result.length()<1) return 0;
if(result.length()==1&&((result.charAt(0)>57&&result.charAt(0)<48))) return 0;
if(result!=""&&((result.charAt(0)=='-'&&result.length()>1)||result.charAt(0)!='-')){
if(result.charAt(0)=='-'){ //负
if(result.length()>11){ //负越界
return Integer.MIN_VALUE;
}else{
l=Long.valueOf(result);
if(l<Integer.MIN_VALUE){
return Integer.MIN_VALUE;
}else{
return Integer.parseInt(result);
}
}
}else{//正
if(result.length()>10){ //正越界
return Integer.MAX_VALUE;
}else{
l=Long.valueOf(result);
if(l>Integer.MAX_VALUE){
return Integer.MAX_VALUE;
}else{
return Integer.parseInt(result);
}
}
}
}
return 0;
}
}
字符串转数字练习--String to Integer (atoi)的更多相关文章
- 【LeetCode 8_字符串_实现】String to Integer (atoi)
, INVALID}; int g_status; long long SubStrToInt(const char* str, bool minus) { ; : ; while (*str != ...
- String to Integer (atoi) - 字符串转为整形,atoi 函数(Java )
String to Integer (atoi) Implement atoi to convert a string to an integer. [函数说明]atoi() 函数会扫描 str 字符 ...
- Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)
Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...
- LeetCode 8. 字符串转换整数 (atoi)(String to Integer (atoi))
8. 字符串转换整数 (atoi) 8. String to Integer (atoi) 题目描述 LeetCode LeetCode8. String to Integer (atoi)中等 Ja ...
- 【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--No.008 String to Integer (atoi)
8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...
- LeetCode【8】. String to Integer (atoi) --java实现
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- [leetcode] 8. String to Integer (atoi) (Medium)
实现字符串转整形数字 遵循几个规则: 1. 函数首先丢弃尽可能多的空格字符,直到找到第一个非空格字符. 2. 此时取初始加号或减号. 3. 后面跟着尽可能多的数字,并将它们解释为一个数值. 4. 字符 ...
随机推荐
- 痞子衡嵌入式:超级好用的可视化PyQt GUI构建工具(Qt Designer)
大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是PyQt GUI构建工具Qt Designer. 痞子衡开博客至今已有好几年,一直以嵌入式开发相关主题的文章为主线,偶尔穿插一些其他技术 ...
- redis增删查改数据Util
目录 (1)需要导入的包 (2)redis配置文件 (3)RedisUtil类 (1)需要导入的包 <dependency> <groupId>org.springframew ...
- windows一机多装mysql,5.5+版本,8.0.11版本
00.安装第一个mysql,压缩版或者安装版,过程省略,目录如下: 运行中输入“regedit” 进入后,找到 HKEY_LOCAL_MACHINE\SYSTEM\CurrentCont ...
- 浅析 jQuery 内部架构设计
jQuery 对于大家而言并不陌生,因此关于它是什么以及它的作用,在这里我就不多言了,而本篇文章的目的是想通过对源码简单的分析来讨论 jQuery 的内部架构设计,以及 jQuery 是如何利用Jav ...
- Pytorch系列教程-使用字符级RNN对姓名进行分类
前言 本系列教程为pytorch官网文档翻译.本文对应官网地址:https://pytorch.org/tutorials/intermediate/char_rnn_classification_t ...
- Asp.net Core的Swagger接口根据模块、版本分组
近期一直在学习Asp.net Core,微软的文档太难看,都是英文翻译过来的,很不友好,感谢这个博客,从壹开始前后端分离[ .NET Core2.0 +Vue2.0 ],让我入门了,刚学到这个Swag ...
- 爬虫框架之Scrapy(二)
递归解析 糗事百科递归解析 在前面的例子里只是爬取了糗事百科热门的第一个页面,但是当我们需要爬取更多的页面时,需要对每个页面的url依次发起请求,然后通过解析的方法进行作者和标题的解析. 我们可以构建 ...
- load data infile
mysql> load data local infile 'd:/lw.txt' into table user (code,password,channelId);Query OK, 478 ...
- SpringBoot进阶教程(二十三)Linux部署Quartz
在之前的一篇文章中<SpringBoot(九)定时任务Schedule>,已经详细介绍了关于schedule框架的配置和使用,有收到一些朋友关于部署的私信,所以抽时间整理一个linux部署 ...
- ByteView和Sink
久违啦米娜桑!!! 最近有点忙,一月有余没有更新了,实在对不起大家!!! 上线后看到不少朋友发的私信,感谢大家的赞许与信任,后面我会尽最大的努力按时更新,不断推出更优质的文章!!! 本来计划最后花1讲 ...