[leetcode]8. 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, 2^31 − 1]. If the numerical value is out of the range of representable values, INT_MAX (2^31 − 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.
题目
字符串转整数
思路
小心各种corner case
代码
- class Solution {
- public int myAtoi(String str) {
- str = str.trim(); // handle str = " "的情况, 此时str.length() = 4, 不会走第一个if语句,从而会造成后续指针的outofbound
- if (str == null || str.length() == 0)
- return 0;
- char c = str.charAt(0);
- int sign = 1, start = 0, len = str.length();
- long sum = 0;
- // take care of sign
- if (c == '+') {
- sign = 1;
- start++;
- } else if (c == '-') {
- sign = -1;
- start++;
- }
- for (int i = start; i < len; i++) {
- // take care of non numerical digit, like 'w'
- if (!Character.isDigit(str.charAt(i))){
- return (int) sum * sign;
- }
- // convert each char to each digit
- sum = sum * 10 + str.charAt(i) - '0';
- // Input: "-91283472332" Output:-2147483648
- if (sign == 1 && sum > Integer.MAX_VALUE){
- return Integer.MAX_VALUE;
- }
- //思考为何不能写成 if(sign == -1 && sum > Integer.MAX_VALUE)
- if (sign == -1 && (-1) * sum < Integer.MIN_VALUE){
- return Integer.MIN_VALUE;
- }
- }
- return (int) sum * sign;
- }
- }
注意: line11-14 显得很多余,因为通常正整数前不会专门写 ‘+’ 号。 但确实有这样的test case(如下图), 所以写上这个条件,会更加严谨。
[leetcode]8. String to Integer (atoi)字符串转整数的更多相关文章
- [LeetCode] 8. String to Integer (atoi) 字符串转为整数
Implement atoi which converts a string to an integer. The function first discards as many whitespace ...
- 【LeetCode】String to Integer (atoi)(字符串转换整数 (atoi))
这道题是LeetCode里的第8道题. 题目要求: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们 ...
- [LeetCode] String to Integer (atoi) 字符串转为整数
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- 【LeetCode】8. String to Integer (atoi) 字符串转换整数
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字符串转整数,atoi,题解,Leetcode, 力扣,P ...
- Leetcode8.String to Integer (atoi)字符串转整数(atoi)
实现 atoi,将字符串转为整数. 该函数首先根据需要丢弃任意多的空格字符,直到找到第一个非空格字符为止.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连续的数字组合起来,这部分字 ...
- 【LeetCode】8. String to Integer (atoi) 字符串转整数
题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...
- Leetcode 8 String to Integer (atoi) 字符串处理
题意:将字符串转化成数字. 前置有空格,同时有正负号,数字有可能会溢出,这里用long long解决(leetcode用的是g++编译器),这题还是很有难度的. class Solution { pu ...
- LeetCode OJ String to Integer (atoi) 字符串转数字
#include <iostream> #include <assert.h> using namespace std; int ato(const char *str) { ...
- 008 String to Integer (atoi) 字符串转换为整数
详见:https://leetcode.com/problems/string-to-integer-atoi/description/ 实现语言:Java class Solution { publ ...
随机推荐
- Centos6.5安装mariadb的坑坑
最近在看Ansible,<Ansible权威指南>,然后有个地方是搭建Web应用框架,有个服务器是安装Mariadb,找到官方文档,一直弄,总是报错,换个思路,下载rpm到本地,安装,然后 ...
- Vue 移动端常用tap事件封装
基于Vue的移动端项目,有些时间原生并没用提供,需要我们自己手动封装,可以封装一些自定义指令来供全局使用. 本文封装了 tap, swipe, swipeleft, swiperight, swipe ...
- 3.2 MathType插入竖省略号
法1:鼠标单击位于上方菜单栏下面的空格和椭圆模版,然后在下拉模版下选择垂直省略号,如下图所示. 按照上面的操作选择垂直省略号模版后,这样在下面的编辑区域就会打出该符号了. 法2:在插入符号下查找 如果 ...
- Python字符串列表元祖字典的公共方法
运算符 运算符 Python 表达式 结果 描述 支持的数据类型 + [1, 2] + [3, 4] [1, 2, 3, 4] 合并 字符串.列表.元组 * 'Hi!' * 4 ['Hi!', 'Hi ...
- 学习vue容易忽视的细节
1.对于自定义标签名(组件名称),Vue.js 不强制要求遵循 W3C 规则 (小写,并且包含一个短杠),尽管遵循这个规则比较好.HTML 特性是不区分大小写的.所以,当使用的不是字符串模板,came ...
- Java遍历Map对象的方式
public static void main(String[] args) { HashMap<String, String> testMap = new HashMap<> ...
- 学习笔记之Python爬虫
Python 爬虫介绍 | 菜鸟教程 http://www.runoob.com/w3cnote/python-spider-intro.html https://blog.csdn.net/sina ...
- Vue+webpack项目配置便于维护的目录结构
新建项目的时候创建合理的目录结构便于后期的维护是很重要 环境:vue.webpack 目录结构: 项目子目录结构 子目录结构都差不多,主要目录是在src下面操作 src目录结构 src/common ...
- day40数据库之表的相关操作
数据库之表的相关操作1.表的操作: 1.创建表的语法: create table 表名( id int(10) primary key auto_inc ...
- Java中字段、属性、成员变量、局部变量、实例变量、静态变量、类变量、常量
首先看个例子: package zm.demo; public class Demo { private int Id;//成员变量(字段).实例变量(表示该Id变量既属于成员变量又属于实例变量) p ...