日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

String to Integer (atoi) leetcode java

發布時間:2025/6/15 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.

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.

?

?

題解

?這題主要就是考慮一下corner case。

?越界問題?

?正負號問題?

?空格問題?

?精度問題?

?代碼如下:

?

?1????public?int?atoi(String?str)?{
?2?????if?(str?==?null?||?str.length()?<?1)
?3?????????return?0;
?4??
?5?????//?trim?white?spaces
?6?????str?=?str.trim();
?7??
?8?????char?flag?=?'+';
?9??
10?????//?check?negative?or?positive
11?????int?i?=?0;
12?????if?(str.charAt(0)?==?'-')?{
13?????????flag?=?'-';
14?????????i++;
15?????}?else?if?(str.charAt(0)?==?'+')?{
16?????????i++;
17?????}
18?????//?use?double?to?store?result
19?????double?result?=?0;
20??
21?????//?calculate?value
22?????while?(str.length()?>?i?&&?str.charAt(i)?>=?'0'?&&?str.charAt(i)?<=?'9')?{
23?????????result?=?result?*?10?+?(str.charAt(i)?-?'0');
24?????????i++;
25?????}
26??
27?????if?(flag?==?'-')
28?????????result?=?-result;
29??
30?????//?handle?max?and?min
31?????if?(result?>?Integer.MAX_VALUE)
32?????????return?Integer.MAX_VALUE;
33??
34?????if?(result?<?Integer.MIN_VALUE)
35?????????return?Integer.MIN_VALUE;
36??
37?????return?(int)?result;
38?}

?

更新:

因為遇見過了atol,string to long 這樣的問題,就不能用這種比當前數據類型長的方法解決。

另一種方法是更加普遍和巧妙的,就是用這樣的判斷:

1. Integer.MAX_VALUE/10 < result; //當前轉換結果比Integer中最大值/10還大(因為這個判斷放在while循環最開始,之后還要對result進行*10+當前遍歷元素的操作,所以如果還乘10的result已經比Integer.MAX_VALUE/10還大,可想而知,乘了10更大)

2. Integer.MAX_VALUE/10 == result && Integer.MAX_VALUE%10 <(str.charAt(i) - '0') //另外的情況就是,當前result恰跟 Integer.MAX_VALUE/10相等,那么就判斷當前遍歷的元素值跟最大值的最后一位的大小關系即可

?

代碼如下:

?1???public?int?atoi(String?str)?{
?2?????if?(str?==?null?||?str.length()?<?1)
?3?????????return?0;
?4??
?5?????//?trim?white?spaces?at?beginning?and?end
?6?????str?=?str.trim();
?7??
?8?????char?flag?=?'+';
?9??
10?????//?check?negative?or?positive
11?????int?i?=?0;
12?????if?(str.charAt(0)?==?'-')?{
13?????????flag?=?'-';
14?????????i++;
15?????}?else?if?(str.charAt(0)?==?'+')?{
16?????????i++;
17?????}
18?????//?use?double?to?store?result
19?????int?result?=?0;
20??
21?????//?calculate?value
22?????while?(str.length()?>?i?&&?str.charAt(i)?>=?'0'?&&?str.charAt(i)?<=?'9')?{
23?????????if(Integer.MAX_VALUE/10?<?result?||?(Integer.MAX_VALUE/10?==?result?&&?Integer.MAX_VALUE%10?<?(str.charAt(i)?-?'0')))?
24?????????????return?flag?==?'-'???Integer.MIN_VALUE?:?Integer.MAX_VALUE;
25?????????????
26?????????result?=?result?*?10?+?(str.charAt(i)?-?'0');
27?????????i++;
28?????}
29??
30?????if?(flag?==?'-')
31?????????result?=?-result;
32??
33?????return?result;
34?}

?

?

?

?

Reference: http://www.programcreek.com/2012/12/leetcode-string-to-integer-atoi/

?

總結

以上是生活随笔為你收集整理的String to Integer (atoi) leetcode java的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。