Leetcode8--->String to Integer(实现字符串到整数的转换)

题目: 实现字符串到整数的转换

解题思路:

下面给出这道题应该注意的一些细节:

1. 字符串“    123   ” = 123;

2.   字符串“+123” = 123;

3.   字符串“-123” = -123;

4.   字符串“-” = 0;“+” = 0;

5.   字符串“123-” = 123;

6.   字符串“21474836478” = 2147483647(Integer.MAX_VALUE)

7.   其余情况都是非法输入,一律返回0;

代码如下:

 1 public class Solution {
 2     public int myAtoi(String str) {
 3         if(str == null || str.length() < 1)
 4             return 0;
 5         boolean negative = false;
 6         int i = 0;
 7         double res = 0.0;
 8         String s = str.trim();
 9         int length = s.length();
10         if(s.charAt(i) < ‘0‘){
11             if(s.charAt(i) == ‘-‘){
12                 negative = true;
13             }
14             else if(s.charAt(i) != ‘+‘){
15                 return 0;
16             }
17             if(length == 1)
18                 return 0;
19             i++;
20         }
21         while(i < length){
22             int n = (s.charAt(i) - ‘0‘) ;
23             if(n >= 0 && n <= 9){
24                 res *= 10;
25                 res += n;
26                 i ++;
27             }
28             else
29                 break;
30
31         }
32         if(negative){
33             res *= -1;
34         }
35         res = res >= Integer.MAX_VALUE ? Integer.MAX_VALUE : res;
36         res = res <= Integer.MIN_VALUE ? Integer.MIN_VALUE : res;
37         return (int)res;
38     }
39 }
时间: 2024-10-02 21:42:57

Leetcode8--->String to Integer(实现字符串到整数的转换)的相关文章

[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) 字符串转整数

Implement atoi to convert a string to an integer. 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. 题目:实现字符串转整数 注意事项:考虑好各种可能的输入(坑); public class Solution

[LeetCode]41. 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】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

leetcode——String to Integer (atoi) 字符串转换为整型数(AC)

mnesia在频繁操作数据的过程可能会报错:** WARNING ** Mnesia is overloaded: {dump_log, write_threshold},可以看出,mnesia应该是过载了.这个警告在mnesia dump操作会发生这个问题,表类型为disc_only_copies .disc_copies都可能会发生. 如何重现这个问题,例子的场景是多个进程同时在不断地mnesia:dirty_write/2 mnesia过载分析 1.抛出警告是在mnesia 增加dump

Java数据类型中String、Integer、int相互间的转换

1.Integer转换成int的方法 Integer i;  int k = i.intValue(); 即Integer.intValue(); 2.int转换成Integer int i; Integer it = new Integer(i); 3.String转换成int的方法 String str = "10";   Integer it = new Interger(str); int i = it.intValue(); 即:int i = Integer.intValu

leetCode 8. String to Integer (atoi) 字符串

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 intende

8. String to Integer (atoi) 字符串转为int类型的所有可能情况

8. String to Integer (atoi) 问题: 输入一个字符串,将字符串转为int类型,处理所有可能的输入情况. 可能的输入情况: 1.字符串为空.即"". 2.首先是假设输入的字符串都是数字型的,可含正负号,例如12345,+12548,-15568. 3.字符串中含有非数字的其他字符类型,例如a,-sd,-1286dgg558,000822fg55. 4.字符串首尾含有空格的,例如 -558dg12, 12dc12 . 5.针对转换后的数字越界,有两种情况,一个是超

[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