1. 程式人生 > >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 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.

spoilers alert… click to show requirements for atoi.

Requirements for atoi:
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.

題目大意

  • 1." +123" 輸出 123
  • 1." -123abcde" 輸出 -123
  • 1." +abc123" 輸出 0
  • 1." " 輸出 0

解題思路

1.先去掉前面的空格,然後看有沒有正負號,然後輸出數字直到遇到非數字則停止。

AC程式碼(java)

package test;
class Solution{
    public int atoi(String str) {
        str = str.trim();
        if(str.isEmpty()){
            return 0;
        }
        int flag = 1;
        int i =0;
        if(str.charAt(i)=='+'){
            i++;
        }else if(str.charAt(i)=='-'){
            flag = -1;
            i++;
        }
        //提防result超出int範圍
        double result = 0.0;
        while(i<str.length()&&str.charAt(i)<='9'&&str.charAt(i)>='0'){
            result = result*10 + str.charAt(i) - '0';
            i++;
        }
        result = flag*result;
        if(result>Integer.MAX_VALUE){
            return Integer.MAX_VALUE;
        }
        if(result<Integer.MIN_VALUE){
            return Integer.MIN_VALUE;
        }
        return (int)result;
    }
}
public class test {
public  static void main(String[] args){
    String a = new String("  -0000123");
    Solution b = new Solution();
    int c = b.atoi(a);
    System.out.println(c);
}
}

相關推薦

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,

[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 firs

Leetcode 8. String to Integer (atoi) 字串轉換整數 (atoi)

Leetcode 8. String to Integer (atoi) 字串轉換整數 (atoi) 標籤: Leetcode 題目地址:https://leetcode-cn.com/problems/string-to-integer-atoi/ 題目描述 請你

LeetCode 8. String to Integer (atoi) C++ --字串轉為數字,包含正負號、空格、字母、數字等字元

Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cas

Leetcode#8. String to Integer (atoi)(字串轉數字)

宣告:題目解法使用c++和Python兩種,重點側重在於解題思路和如何將c++程式碼轉換為python程式碼。 題目 Implement atoi to convert a string to an integer. Hint: Carefully con

8. String to Integer (atoi)

pos ets 標準 rac 思路 cte present digi targe Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input

LeetCode 8. String to Integer (atoi)

req argument require git empty convert int oss 非法字符 Implement atoi to convert a string to an integer. Hint: Carefully consider all possib

Leetcode:8- String to Integer (atoi)

integer font please because sta digits 翻譯 isspace time Implement atoi to convert a string to an integer. Hint: Carefully consider all pos

leetcode-8. String to Integer (atoi)

then ble sig 記得 repr exist elf sta eric 1 題目 Implement atoi to convert a string to an integer. Hint: Carefully consider all possible

8. String to Integer(Atoi)

題目: 解答: 關鍵是注意怎麼判定溢位的問題,測試用例一定要保證有INT_MAX, INT_MIN, INT_MAX + 1, INT_MIN - 1; 當然,如果result設定為long long就完全沒問題了,但是這樣做就太沒有難度了 程式碼: class S

[leetcode]8. String to Integer (atoi)

這一題在思路上還是比較簡單。 注意邊界情況。 結合上一個reverse integer來看,要注意處理溢位 for(int m=0;m<j;m++){ if(res>(Integer.MAX_VALUE-(int)k[m]+48)/10

LeetCode 8 String to Integer(atoi)

8 String to Integer(atoi) 按照題目的要求:你要先越過前面的空格。 之後可能遇到’+’’-’ [0-9],other,也照常處理即可 還要注意溢位問題:這裡我用了long long(感覺有點作弊了QwQ)。當int溢位了,long long肯定沒溢位.所以你在轉換

【LeetCode】8. String to Integer (atoi) - Java實現

文章目錄 1. 題目描述: 2. 思路分析: 3. Java程式碼: 1. 題目描述: Implement atoi which converts a string to an integer. The function fir

【leetcode】8. String to Integer (atoi)(從string中按照規則提取int)

Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary until

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 fi

演算法設計與分析課作業【week6】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 th

LeetCode——8. String to Integer (atoi)

1.題目詳情 Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessar

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

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

8. String to Integer (atoi) - Medium

Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary until the firs