字符串转数字练习--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. 字符 ...
随机推荐
- Vue.js 学习笔记 第1章 初识Vue.js
本篇目录: 1.1 Vue.js 是什么 1.2 如何使用Vue.js 本章主要介绍与Vue.js有关的一些概念与技术,并帮助你了解它们背后相关的工作原理. 通过对本章的学习,即使从未接触过Vue.j ...
- GIS之家demo源代码咨询
GIS之家demo源代码咨询收费服务(希望对 webgis 新人有所帮助) GIS之家QQ群(采取QQ群入群收费模式): GIS之家001:296438295 需要入群的giser们,入群之前联系GI ...
- Flutter 即学即用系列博客——07 RenderFlex overflowed 引发的思考
背景 在进行 Flutter UI 开发的时候,控制台报出了下面错误: flutter: ══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY >╞════════ ...
- nginx 安装、启动、重启、关闭 (linux系统命令行)
前言: 最近在部署我的hexo静态博客到腾讯云服务器上,用到了很多nginx的知识,在此做下总结: 刚接触的linux服务器上,nginx配置乱的有点令人发指,就把老的卸载了重新装一下. 1.卸载 y ...
- nginx解决反向代理超时
最近在公司windows服务器部署nginx前端项目时 因为业务需求 有个有个接口数据量很大,请求时长在很大可能超过一分钟 然后一直遇到了504 Gateway Time-out 在网上查了很多资料都 ...
- 【机器学习】--Adaboost从初始到应用
一.前述 AdaBoost算法和GBDT(Gradient Boost Decision Tree,梯度提升决策树)算法是基于Boosting思想的机器学习算法.在Boosting思想中是通过对样本进 ...
- Python基础(random模块)
random 常用的方法: #Author : Kelvin #Date : 2019/1/6 15:33 import random print(random.random()) #产生0-1之间的 ...
- 【Netty】(4)—源码AbstractBootstrap
源码AbstractBootstrap 一.概念 AbstractBootstrap是一个工具类,用于服务器通道的一系列配置,绑定NioEventLoopGroup线程组,指定指定NIO的模式,指定子 ...
- python中的编码与解码
编码与解码 首先,明确一点,计算机中存储的信息都是二进制的 编码/解码本质上是一种映射(对应关系),比如‘a’用ascii编码则是65,计算机中存储的就是00110101,但是显示的时候不能显 ...
- apriori && fpgrowth:频繁模式与关联规则挖掘
已迁移到我新博客,阅读体验更佳apriori && fpgrowth:频繁模式与关联规则挖掘 详细代码我放在github上:click me 一.实验说明 1.1 任务描述 1.2 数 ...