字符串转数字练习--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. 字符 ...
随机推荐
- [转]POJ1006: 中国剩余定理的完美演绎
Biorhythms Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 117973 Accepted: 37026 Des ...
- 一个经典的 HTTP协议详解
1引言 HTTP是一个属于应用层的面向对象的协议,由于其简捷.快速的方式,适用于分布式超媒体信息系统.它于1990年提出,经过几年的使用与发展,得到不断地完善和扩展.目前在WWW中使用的是HTTP/1 ...
- IDEA创建applicationContext.xml 无法自动提示,文件图标是文本类型
问题:创建applicationContext.xml 的时候注册到file里边去了. 解决方法: 打开设置界面找到以下界面: 删除掉 Text 里边的 applicationContext.xml ...
- Java多线程系列(1)
本章主要内容有: 1.线程进程的区别 2.线程的生命周期 3.Java内存模型 原子性,可见性及有序性 4.线程池及Java实现 1. 线程进程的区别 线程:程序运行的最小单位 进程:资源分配的最小单 ...
- CTF取证方法大汇总,建议收藏!
站在巨人的肩头才会看见更远的世界,这是一篇来自技术牛人的神总结,运用多年实战经验总结的CTF取证方法,全面细致,通俗易懂,掌握了这个技能定会让你在CTF路上少走很多弯路,不看真的会后悔! 本篇文章大约 ...
- T5大牛带你解析:如何实现分布式技术
1.分布式事务 2. 分布式锁 Java 原生 API 虽然有并发锁,但并没有提供分布式锁的能力,所以针对分布式场景中的锁需要解决的方案. 分布式锁的解决方案大致有以下几种: 基于数据库实现 基于缓存 ...
- 重新发现MATLAB
现场编辑 创建不仅捕获代码的脚本 - 它们讲述了可以与他人共享的故事.自动化的上下文提示可让您在编程时快速移动,并将结果和可视化与代码一起显示. 学到更多 访问MATLAB实时脚本库 ...
- C# 《编写高质量代码改善建议》整理&笔记 --(三)泛型&委托&事件
1.泛型 基于泛型,我们可以将类型参数化,以便更大范围地进行代码复用.同时,它减少了泛型类及泛型方法中的转型, 确保类型安全. 1)总是优先考虑泛型 优点:可重用性,类型安全,高效率. 2)避免在泛型 ...
- DotNetCore跨平台~聊聊中间件
回到目录 在进行.net core平台之后,我们如果希望在请求过程中添加一些事件是非常容易的,你可以把这些事件做成一个中间件Middleware,然后这些中间件就会以Http pipeline的管道方 ...
- Docker进阶之五:容器管理
容器管理 一.创建容器常用选项 docker container --help 指令 描述 资源限制指令 -i, --interactive 交互式 -m,--memory 容器可以使用的最大内存量 ...