LeetCode 8 String to Integer (string转int)

题目来源:https://leetcode.com/problems/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.

Update (2015-02-10):
The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button  to reset your code definition.

解题思路:

照着要求写代码,可以总结如下:

1. 字串为空或者全是空格,返回0;

2. 字串的前缀空格需要忽略掉;

3. 忽略掉前缀空格后,遇到的第一个字符,如果是‘+’或‘-’号,继续往后读;如果是数字,则开始处理数字;如果不是前面的2种,返回0;

4. 处理数字的过程中,如果之后的字符非数字,就停止转换,返回当前值;

5. 在上述处理过程中,如果转换出的值超出了int型的范围,就返回int的最大值或最小值。

Java代码:

 1 public class Solution {
 2     public int myAtoi(String str) {
 3         int max = Integer.MAX_VALUE;
 4         int min = -Integer.MIN_VALUE;
 5         long result = 0;
 6         str = str.trim();
 7         int len = str.length();
 8         if (len < 1)
 9             return 0;
10         int start = 0;
11         boolean neg = false;
12
13         if (str.charAt(start) == ‘-‘ || str.charAt(start) == ‘+‘) {
14             if (str.charAt(start) == ‘-‘)
15                 neg = true;
16             start++;
17         }
18
19         for (int i = start; i < len; i++) {
20             char ch = str.charAt(i);
21
22             if (ch < ‘0‘ || ch > ‘9‘)
23                 break;
24             result = 10 * result + (ch - ‘0‘);
25             if (!neg && result > max)
26                 return max;
27             if (neg && -result < min)
28                 return min;
29
30         }
31         if (neg)
32             result = -result;
33
34         return (int) result;
35     }
36
37 };
时间: 2024-10-02 07:27:47

LeetCode 8 String to Integer (string转int)的相关文章

LeetCode:String to Integer (atoi)

1.题目名称 String to Integer (atoi) (字符串到数字的转换) 2.题目地址 https://leetcode.com/problems/string-to-integer-atoi/ 3.题目内容 英文:Implement atoi to convert a string to an integer. 中文:实现atoi函数,将输入的字符串(String类型)转换为整型数据(Integer类型) 提示:实现的atoi函数需要满足以下特征 忽略字符串第一个非空格字符前的所

Leetcode 数 String to Integer (atoi)

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie String to Integer (atoi) Total Accepted: 9862 Total Submissions: 67880 Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge,

【LeetCode】- String to 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). Y

LeetCode 008 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 b

[LeetCode] 008. String to Integer (Easy) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 008.String_to_Integer (Easy) 链接: 题目:https://oj.leetcode.com/problems/string-to-integer-atoi/ 代码(github):https://github.com/illuz/leetcode 题意: 将一个字符串转化为 int 型.

【leetcode系列】String to Integer (atoi)

这个我就直接上代码了,最开始把"abc123"也算作合法的了,后来查了一下atoi的定义,把这种去掉了. public class Solution { public static int atoi(String inStr) { long result = 0L; /* * 网上查了一下,atoi函数的定义是如果第一个非空格字符存在,是数字或者正负号则开始做类型转换, * 之后检测到非数字(包括结束符\0)字符时停止转换,返回整型数.否则,返回零.可能的输入情况有: 1.空字符串 *

String to Integer (atoi) leetcode java

题目: 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

【leetcode刷题笔记】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 spe

【LeetCode】String to Integer (atoi) 解题报告

这道题在LeetCode OJ上难道属于Easy.可是通过率却比較低,究其原因是须要考虑的情况比較低,非常少有人一遍过吧. [题目] 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 pos