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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

408. Valid Word Abbreviation有效的单词缩写

發布時間:2025/3/21 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 408. Valid Word Abbreviation有效的单词缩写 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

[抄題]:

Given a?non-empty?string?s?and an abbreviation?abbr, return whether the string matches with the given abbreviation.

A string such as?"word"?contains only the following valid abbreviations:

["word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d", "wo2", "1o1d", "1or1", "w1r1", "1o2", "2r1", "3d", "w3", "4"]

Notice that only the above abbreviations are valid abbreviations of the string?"word". Any other string is not a valid abbreviation of?"word".

Note:
Assume?s?contains only lowercase letters and?abbr?contains only lowercase letters and digits.

Example 1:

Given s = "internationalization", abbr = "i12iz4n":Return true.

?

Example 2:

Given s = "apple", abbr = "a2e":Return false.

?[暴力解法]:

時間分析:

空間分析:

?[優化后]:

時間分析:

空間分析:

[奇葩輸出條件]:

[奇葩corner case]:

  • 第零位長度就是1了,指針要先加再給 ++i 提前加一 , 需要再次控制范圍:j < abbr.length() 否則會不自覺溢出
  • 此題特殊:j所在數字為0也不行,01會返回true "a" "01"
  • [思維問題]:

    兩個單詞居然會想不出兩個指針嗎?

    [一句話思路]:

  • 沒數字時一直走,走到有數字時再開始統計
  • [輸入量]:空:?正常情況:特大:特小:程序里處理到的特殊情況:異常情況(不合法不合理的輸入):

    [畫圖]:

    [一刷]:

    [二刷]:

    [三刷]:

    [四刷]:

    [五刷]:

    ? [五分鐘肉眼debug的結果]:

    [總結]:

    兩個單詞就用兩個指針,兩個起點

    [復雜度]:Time complexity: O() Space complexity: O()

    [英文數據結構或算法,為什么不用別的數據結構或算法]:

    ASCII碼表的順序是(按二進制排序):數字-大寫字母-小寫字母

    [關鍵模板化代碼]:

    [其他解法]:

    [Follow Up]:

    [LC給出的題目變變變]:

    527.?Word Abbreviation 自制單詞壓縮:要排序

    411.?Minimum Unique Word Abbreviation 排列組合找出第一個單詞的所有縮寫:回溯法

    ?[代碼風格] :

    class Solution {public boolean validWordAbbreviation(String word, String abbr) {//iniint i = 0, j = 0;//while loopwhile (i < word.length() && j < abbr.length()) {//if letters, go onif (word.charAt(i) == abbr.charAt(j)) {++i;++j;continue;}//cc, first num shouldn't be 0if (abbr.charAt(j) <= '0' || abbr.charAt(j) > '9') return false;//substring of jint start = j;//control j wheneverwhile (j < abbr.length() && abbr.charAt(j) >= '0' && abbr.charAt(j) <= '9') {++j;}int num = Integer.valueOf(abbr.substring(start, j));//add to ii += num;}//returnreturn (i == word.length()) && (j == abbr.length());} } View Code

    ?

    轉載于:https://www.cnblogs.com/immiao0319/p/8651383.html

    總結

    以上是生活随笔為你收集整理的408. Valid Word Abbreviation有效的单词缩写的全部內容,希望文章能夠幫你解決所遇到的問題。

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