Java [leetcode 8] String to Integer (atoi)
问题描述:
Implement atoi to convert a string to an integer.
Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.
Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.
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. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.
解题思路:
首先调用trim方法除去字符串两边的空白字符。然后从开头读起,如果开头为‘+’,表明该值为正数;如果开头为‘-’,表明该值为负数;如果第一个读到的是‘+’或者‘-’,则从第二个开始读,否则仍然从第一个读起。下面开始一直往后读直到字符串结束。如果在读的过程中读到了除字符’0‘-’9'以外的数字,那么跳出循环,返回计算好的值,否则,不停的计算累加值。
代码如下:
public class Solution {
public int myAtoi(String str) {
boolean negative = false;
int i = 0;
int result = 0;
int digit; if (str == null || str.length() == 0)
return 0;
str = str.trim();
if (str.length() == 0)
return 0;
if (str.charAt(0) == '-' || str.charAt(0) == '+') {
if (str.charAt(0) == '-') {
negative = true;
}
i++;
}
while (i < str.length()) {
if (str.charAt(i) > '9' || str.charAt(i) < '0')
break;
digit = str.charAt(i) - '0';
if (!negative && result > (Integer.MAX_VALUE - digit) / 10)
return Integer.MAX_VALUE;
else if (negative && result > -((Integer.MIN_VALUE + digit) / 10))
return Integer.MIN_VALUE;
else {
result = result * 10 + digit;
i++;
}
}
return negative ? -result : result;
}
}
Java [leetcode 8] String to Integer (atoi)的更多相关文章
- Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)
Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...
- leetcode day6 -- String to Integer (atoi) && Best Time to Buy and Sell Stock I II III
1. String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully con ...
- 【leetcode】String to Integer (atoi)
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. 字符 ...
- Leetcode 8. String to Integer (atoi)(模拟题,水)
8. String to Integer (atoi) Medium Implement atoi which converts a string to an integer. The functio ...
- [LeetCode][Python]String to Integer (atoi)
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/string- ...
- 【JAVA、C++】 LeetCode 008 String to Integer (atoi)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- 【LeetCode】String to Integer (atoi) 解题报告
这道题在LeetCode OJ上难道属于Easy.可是通过率却比較低,究其原因是须要考虑的情况比較低,非常少有人一遍过吧. [题目] Implement atoi to convert a strin ...
- LeetCode 8. String to Integer (atoi) (字符串到整数)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
随机推荐
- sv_target_output dx11
http://msdn.microsoft.com/en-us/library/windows/desktop/bb509647(v=vs.85).aspx
- ${fn:length(worklicenseList)} #表示不在struts堆栈里,没有#表示从struts堆栈里取
${fn:length(worklicenseList)} #表示不在struts堆栈里,没有#表示从struts堆栈里取
- JS事件(事件冒泡和事件捕获)
事件流:描述的是在页面中接收事件的顺序 事件冒泡:由最具体的元素接收,然后逐级向上传播至最不具体的元素的节点(文档) 事件捕获:最不具体的节点先接收事件,而最具体的节点应该是最后接收事件 DOM中:用 ...
- 如何配置JAVA的环境变量、Tomcat环境变量
配置JAVA环境变量 1.右击[我的电脑]---[属性]-----[高级]---[环境变量],如图: 2.选择[新建系统变量]--弹出“新建系统变量”对话框,在“变量名”文本框输入“JAVA_HOME ...
- error: ‘for’ loop initial declarations are only allowed in C99 mode
比如写出下面这段程序: for (int i = 0; i < n; ++i) do_something(); 然后用gcc编译,会报 ‘for’ loop initial declaratio ...
- java:类集框架
类集框架:jdk提供的一系列类和接口,位于java.util包当中,主要用于存储和管理对象,主要分为三大类:集合.列表和映射. 集合Set:用于存储一系列对象的集合.无序.不允许重复元素. 列表Lis ...
- C#基础练习(时间的三连击)
Form1的后台代码: namespace _07事件的三连击 { public partial class Form1 : Form { public Form1() ...
- Linux下安装、配置、启动Apache
http://www.cnblogs.com/zhuque/archive/2012/11/03/2763352.html#
- 3、JPA一些常用的注解
常用注解有下面这些: ①:@Entity.@Table.@Id.@GeneratedValue.@Column.@Basic ②:@Transient 用于忽略某个属性,而不对该属性进行持久化操作 ③ ...
- 各种分区类型对应的partition_Id
ID Name Note == ==== ==== 00h empty [空] 01h DOS 12-bit FAT [MS DOS FAT12] 02h XENIX root file system ...