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)的更多相关文章

  1. 【LeetCode 8_字符串_实现】String to Integer (atoi)

    , INVALID}; int g_status; long long SubStrToInt(const char* str, bool minus) { ; : ; while (*str != ...

  2. String to Integer (atoi) - 字符串转为整形,atoi 函数(Java )

    String to Integer (atoi) Implement atoi to convert a string to an integer. [函数说明]atoi() 函数会扫描 str 字符 ...

  3. Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)

    Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...

  4. LeetCode 8. 字符串转换整数 (atoi)(String to Integer (atoi))

    8. 字符串转换整数 (atoi) 8. String to Integer (atoi) 题目描述 LeetCode LeetCode8. String to Integer (atoi)中等 Ja ...

  5. 【leetcode】String to Integer (atoi)

    String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...

  6. No.008 String to Integer (atoi)

    8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...

  7. LeetCode--No.008 String to Integer (atoi)

    8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...

  8. LeetCode【8】. String to Integer (atoi) --java实现

    String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...

  9. [leetcode] 8. String to Integer (atoi) (Medium)

    实现字符串转整形数字 遵循几个规则: 1. 函数首先丢弃尽可能多的空格字符,直到找到第一个非空格字符. 2. 此时取初始加号或减号. 3. 后面跟着尽可能多的数字,并将它们解释为一个数值. 4. 字符 ...

随机推荐

  1. angular-froala-wysiwyg 富文本编辑器使用及遇到的坑

    介绍: angular-froala-wysiwyg: froala editor 的angular版本,支持Angular 2, Angular 4, Angular 5, Angular 6 an ...

  2. String字符串类总结

    object类 int hashCode() Object定义的hashCode方法能为不同对象返回不同的整数.实际上是把JVM给对象分配的地址转化为整数,确保了逻辑上的唯一性.而转化的散列算法,可能 ...

  3. 《SQL CookBook 》笔记-准备工作

    目录 准备 1.建立员工表--EMP 2.建立部门表--DEPT 3.EMP表和DEPT表插入数据 4.建立透视表T1,并插入数据 5.建立透视表T10,并插入数据 第二章 shanzm 准备 1.建 ...

  4. LeetCode算法题-Rotated Digits(Java实现)

    这是悦乐书的第316次更新,第337篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第185题(顺位题号是788).如果一个数字经过180度旋转后,变成了一个与原数字不同的 ...

  5. 爬虫须知的HTTP协议

    HTTP请求: 1.HTTP请求主要分为"Get"和"Post"两种方法. 2.当我们在浏览器输入URL http://www.baidu.com 的时候, 浏 ...

  6. Linux 使用 free 命令查看内存使用情况

    1.free 命令的选项 使用 free 命令查看服务器内存使用情况. free [-b|-k|-m|-g|-h] [-l] [-o] [-t] [-s delay] [-c count] [-V] ...

  7. 程序员从宏观、微观角度浅析JVM虚拟机!

    1.问题 1.JAVA文本文件如何被翻译成CLASS二进制文件? 2.如何理解CLASS文件的组成结构? 3.虚拟机如何加载使用类文件的生命周期? 4.虚拟机系列诊断工具如何使用? 5.虚拟机内存淘汰 ...

  8. pandas列合并为一行

    将dataframe利用pandas列合并为一行,类似于sql的GROUP_CONCAT函数.例如如下dataframe id_part pred pred_class v_id 0 d 0 0.12 ...

  9. 【深度学习篇】--Seq2Seq模型从初识到应用

    一.前述 架构: 问题: 1.压缩会损失信息 2.长度会影响准确率 解决办法: Attention机制:聚焦模式 “高分辨率”聚焦在图片的某个特定区域并以“低分辨率”,感知图像的周边区域的模式.通过大 ...

  10. Nodejs+Express 搭建 web应用

    简单的记录下关于如何使用nodejs+Express 极速搭建一个web应用. 项目所需,要用到nodejs,那就去学咯.简单的看了下 七天学会NodeJS,Node.js 教程.发现其实好简单的,分 ...