字符串转数字练习--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. 字符 ...
随机推荐
- 设计模式之外观模式——Java语言描述
外观模式隐藏系统的复杂性,并向客户端提供了一个客户端可以访问系统的接口.它想现有的系统添加了一个接口,以隐藏系统的复杂性 介绍 意图 为子系统中的一组接口提供了一个一致的界面,外观模式定义了一个高层接 ...
- Cookie、LocalStorage 与 SessionStorage的区别在哪里?
基本概念 Cookie Cookie 是小甜饼的意思.顾名思义,cookie 确实非常小,它的大小限制为4KB左右.它的主要用途有保存登录信息,比如你登录某个网站市场可以看到“记住密码”,这通常就是通 ...
- 关于windows系统无法更新的解决方法
操作系统无法更新解决方案! 1.检查网络连接是否正常: 2.检查控制面板设置,windows更新是否开启: 3.检查计算机-管理-服务和应用程序-服务组件里的windows update是否开启: 4 ...
- 跟我一起学opencv 第五课之调整图像亮度和对比度
一.调整图像亮度与对比度 1.图像变换 ---像素变换-点操作 ---邻域操作-区域操作 调整图像亮度和对比度属于像素变换-点操作 公式为:g(i,j) = αf(i,j) + β 其中α>0 ...
- Docker & ASP.NET Core (2):定制Docker镜像
上一篇文章:把代码连接到容器 Dockerfile 在Docker的世界里,我们可以通过一个叫Dockerfile的文件来创建Docker镜像,随后可以运行容器. Dockerfile就是一个文本文件 ...
- 10个小技巧助您写出高性能的ASP.NET Core代码
今天这篇文章我们来聊一聊如何提升并优化ASP.NET Core应用程序的性能,本文的大部分内容来自翻译,当然中间穿插着自己的理解,希望对大家有所帮助!话不多说开始今天的主题吧! 我们都知道性能是公共网 ...
- 时序数据库连载系列:指标届的独角兽Prometheus
简介 Prometheus是SoundCloud公司开发的一站式监控告警平台,依赖少,功能齐全.于2016年加入CNCF,广泛用于 Kubernetes集群的监控系统中,2018.8月成为继K8S之后 ...
- .NetCore WebAPI采坑之路(持续更新)
1.WebAPI新增日志过滤器or中间件后Action读取到的请求Body为空问题 案例: 自定义了一个中间件,用于记录每次访问webapi的入参,以及引用了Swagger. 先看下面这段代码: pu ...
- Spring Boot 2.x(十一):AOP实战--打印接口日志
接口日志有啥用 在我们日常的开发过程中,我们可以通过接口日志去查看这个接口的一些详细信息.比如客户端的IP,客户端的类型,响应的时间,请求的类型,请求的接口方法等等,我们可以对这些数据进行统计分析,提 ...
- SpringCloud系列——SSO 单点登录
前言 作为分布式项目,单点登录是必不可少的,文本基于之前的的博客(猛戳:SpringCloud系列——Zuul 动态路由,SpringBoot系列——Redis)记录Zuul配合Redis实现一个简单 ...