<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          LeetCode刷題實(shí)戰(zhàn)8:字符串轉(zhuǎn)換整數(shù)

          共 2719字,需瀏覽 6分鐘

           ·

          2020-08-13 22:40

          算法的重要性,我就不多說了吧,想去大廠,就必須要經(jīng)過基礎(chǔ)知識和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個(gè)公眾號后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !


          今天和大家聊的問題叫做字符串轉(zhuǎn)換整數(shù),我們先來看題面:


          Implement atoi which converts a string to an integer.

          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.


          https://leetcode-cn.com/problems/string-to-integer-atoi/

          翻譯


          請你來實(shí)現(xiàn)一個(gè) atoi 函數(shù),使其能將字符串轉(zhuǎn)換成整數(shù)。

          首先,該函數(shù)會根據(jù)需要丟棄無用的開頭空格字符,直到尋找到第一個(gè)非空格的字符為止。接下來的轉(zhuǎn)化規(guī)則如下:

          如果第一個(gè)非空字符為正或者負(fù)號時(shí),則將該符號與之后面盡可能多的連續(xù)數(shù)字字符組合起來,形成一個(gè)有符號整數(shù)。
          假如第一個(gè)非空字符是數(shù)字,則直接將其與之后連續(xù)的數(shù)字字符組合起來,形成一個(gè)整數(shù)。
          該字符串在有效的整數(shù)部分之后也可能會存在多余的字符,那么這些字符可以被忽略,它們對函數(shù)不應(yīng)該造成影響。
          注意:假如該字符串中的第一個(gè)非空格字符不是一個(gè)有效整數(shù)字符、字符串為空或字符串僅包含空白字符時(shí),則你的函數(shù)不需要進(jìn)行轉(zhuǎn)換,即無法進(jìn)行有效轉(zhuǎn)換。

          在任何情況下,若函數(shù)不能進(jìn)行有效的轉(zhuǎn)換時(shí),請返回 0 。

          提示:

          本題中的空白字符只包括空格字符 ' ' 。
          假設(shè)我們的環(huán)境只能存儲 32 位大小的有符號整數(shù),那么其數(shù)值范圍為 [?231, ?231 ? 1]。如果數(shù)值超過這個(gè)范圍,請返回? INT_MAX (231 ? 1) 或 INT_MIN (?231) 。


          樣例


          示例?1:
          輸入: "42"輸出: 42示例?2:
          輸入: " -42"輸出: -42解釋: 第一個(gè)非空白字符為 '-', 它是一個(gè)負(fù)號。? 我們盡可能將負(fù)號與后面所有連續(xù)出現(xiàn)的數(shù)字組合起來,最后得到 -42 。示例?3:
          輸入: "4193 with words"輸出: 4193解釋: 轉(zhuǎn)換截止于數(shù)字 '3' ,因?yàn)樗南乱粋€(gè)字符不為數(shù)字。示例?4:
          輸入: "words and 987"輸出: 0解釋: 第一個(gè)非空字符是 'w', 但它不是數(shù)字或正、負(fù)號。 因此無法執(zhí)行有效的轉(zhuǎn)換。


          解題分析

          這道題問的內(nèi)容不是很深刻,無非就是字符串的簡單處理,使用Java對字符串進(jìn)行處理也是很簡單的,所以就采用了java的算法,不過java這玩意兒相對來說確實(shí)要深一點(diǎn),會用hash和不會用hash完全是兩種水平!


          ? ? 首先我們使用trim()去掉字符串兩端的空格


          ? ? 其次拿出第一個(gè)字符,如果不是+、 -、 數(shù)字 則返回0


          ? ? 否則第一個(gè)合法,入隊(duì)


          ? ? 接著循環(huán)一個(gè)字符一個(gè)字符判斷,如果是數(shù)字就直接入隊(duì),不是則終止循環(huán)


          ? ? 緊接著需要判斷是否只有 +、-號的情況


          ? ? 最后得到數(shù)字字符串隊(duì)列,調(diào)用Integer.parseInt()對隊(duì)列進(jìn)行轉(zhuǎn)換操作,如果遇到異常則處理異常,異常有兩種情況:


          ? ? ? ? ? ?(1).超過最大整數(shù),則返回Integer.MAX_VALUE ( 0x7fffffff )? ??


          ? ? ? ? ? ?(2). 超過最小整數(shù),則 返回Integer.MIN_VALUE ( 0x80000000 )


          class?Solution?{
          ????public?int?myAtoi(String str)?{
          ????????str = str.trim(); //第一步去掉前后的空格
          ????????StringBuilder ans = new?StringBuilder();
          ????????if(str.length() < 1){
          ????????????return?0;
          ????????}
          ????????// 判斷第一個(gè)是什么 是符號或者數(shù)字,其它的話直接返回0
          ????????char?ch = str.charAt(0);
          ????????if(ch != '-'?&& ch != '+'?&& (ch < '0'?|| ch > '9')){
          ????????????return?0;
          ????????}
          ????????//第一個(gè)是合法的
          ????????ans.append(ch);
          ????????//接著,慢慢進(jìn)入字符
          ????????for?(int?i = 1; i < str.length(); i++){
          ????????????if?(str.charAt(i) >= '0'?&& str.charAt(i) <= '9'){
          ????????????????ans.append(str.charAt(i));
          ????????????}else?{
          ????????????????break;
          ????????????}
          ????????}
          ????????//排除只有正負(fù)號的情況
          ????????if?(ans.length() <= 1?&& (ans.charAt(0) == '+'?|| ans.charAt(0) == '-')){
          ????????????return?0;
          ????????}
          ????????//ans就是得到的數(shù)字,但是不知道大小
          ????????try?{
          ????????????return?Integer.parseInt(ans.toString());
          ????????} catch?(NumberFormatException e) {
          ????????????if?(ans.charAt(0) == '-'){
          ????????????????return?Integer.MIN_VALUE;
          ????????????}else?{
          ????????????????return?Integer.MAX_VALUE;
          ????????????}
          ????????}
          ????}
          }



          本題官方還給了另外個(gè)方法:有限狀態(tài)機(jī)?,大家有興趣的自己去leetcode學(xué)習(xí)一下,今天文章就不說了。


          如果喜歡本文,請順手點(diǎn)個(gè)贊或者轉(zhuǎn)發(fā)吧。



          上期推文:


          LeetCode刷題實(shí)戰(zhàn)1:在數(shù)組上遍歷出花樣

          LeetCode刷題實(shí)戰(zhàn)2:用鏈表模擬加法

          LeetCode刷題實(shí)戰(zhàn)3:最長不重復(fù)子串

          LeetCode刷題實(shí)戰(zhàn)4:兩個(gè)正序數(shù)組的中位數(shù)

          LeetCode刷題實(shí)戰(zhàn)5:判斷回文子串

          LeetCode刷題實(shí)戰(zhàn)6:Z字形變換

          LeetCode刷題實(shí)戰(zhàn)7:整數(shù)反轉(zhuǎn)


          瀏覽 29
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評論
          圖片
          表情
          推薦
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  91av免费 | 日日夜夜精选视频 | 青娱乐永久在线视频 | 三级在线激情网 | 日韩中出视频 |