亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

[Leetcode] One Edit Distance 編輯距離為一

lewinlee / 1882人閱讀

摘要:比較長(zhǎng)度法復(fù)雜度時(shí)間空間思路雖然我們可以用的解法,看是否為,但中會(huì)超時(shí)。這里我們可以利用只有一個(gè)不同的特點(diǎn)在時(shí)間內(nèi)完成。

One Edit Distance
Given two strings S and T, determine if they are both one edit distance apart.
比較長(zhǎng)度法 復(fù)雜度

時(shí)間 O(N) 空間 O(1)

思路

雖然我們可以用Edit Distance的解法,看distance是否為1,但Leetcode中會(huì)超時(shí)。這里我們可以利用只有一個(gè)不同的特點(diǎn)在O(N)時(shí)間內(nèi)完成。如果兩個(gè)字符串只有一個(gè)編輯距離,則只有兩種情況:

兩個(gè)字符串一樣長(zhǎng)的時(shí)候,說明有一個(gè)替換操作,我們只要看對(duì)應(yīng)位置是不是只有一個(gè)字符不一樣就行了

一個(gè)字符串比另一個(gè)長(zhǎng)1,說明有個(gè)增加或刪除操作,我們就找到第一個(gè)對(duì)應(yīng)位置不一樣的那個(gè)字符,如果較長(zhǎng)字符串在那個(gè)字符之后的部分和較短字符串那個(gè)字符及之后的部分是一樣的,則符合要求

如果兩個(gè)字符串長(zhǎng)度差距大于1,肯定不對(duì)

代碼
public class Solution {
    public boolean isOneEditDistance(String s, String t) {
        int m = s.length(), n = t.length();
        if(m == n) return isOneModified(s, t);
        if(m - n == 1) return isOneDeleted(s, t);
        if(n - m == 1) return isOneDeleted(t, s);
        // 長(zhǎng)度差距大于2直接返回false
        return false;
    }
    
    private boolean isOneModified(String s, String t){
        boolean modified = false;
        // 看是否只修改了一個(gè)字符
        for(int i = 0; i < s.length(); i++){
            if(s.charAt(i) != t.charAt(i)){
                if(modified) return false;
                modified = true;
            }
        }
        return modified;
    }
    
    public boolean isOneDeleted(String longer, String shorter){
        // 找到第一組不一樣的字符,看后面是否一樣
        for(int i = 0; i < shorter.length(); i++){
            if(longer.charAt(i) != shorter.charAt(i)){
                return longer.substring(i + 1).equals(shorter.substring(i));
            }
        }
        return true;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/64723.html

相關(guān)文章

  • Leetcode[161] One Edit Distance

    摘要:復(fù)雜度思路考慮如果兩個(gè)字符串的長(zhǎng)度,是肯定當(dāng)兩個(gè)字符串中有不同的字符出現(xiàn)的時(shí)候,說明之后的字符串一定要相等。的長(zhǎng)度比較大的時(shí)候,說明的時(shí)候,才能保證距離為。 LeetCode[161] One Edit Distance Given two strings S and T, determine if they are both one edit distance apart. Stri...

    周國輝 評(píng)論0 收藏0
  • [Leetcode] Edit Distance 最小編輯距離

    摘要:動(dòng)態(tài)規(guī)劃復(fù)雜度時(shí)間空間思路這是算法導(dǎo)論中經(jīng)典的一道動(dòng)態(tài)規(guī)劃的題。 Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.) You h...

    zhangke3016 評(píng)論0 收藏0
  • [LintCode/LeetCode] Edit Distance

    摘要:構(gòu)造數(shù)組,是的,是的,是將位的轉(zhuǎn)換成位的需要的步數(shù)。初始化和為到它們各自的距離,然后兩次循環(huán)和即可。 Problem Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 s...

    snowell 評(píng)論0 收藏0
  • LeetCode[72] Edit Distance

    摘要:復(fù)雜度思路考慮用二維來表示變換的情況。如果兩個(gè)字符串中的字符相等,那么如果兩個(gè)字符串中的字符不相等,那么考慮不同的情況表示的是,從字符串到的位置轉(zhuǎn)換到字符串到的位置,所需要的最少步數(shù)。 LeetCode[72] Edit Distance Given two words word1 and word2, find the minimum number of steps require...

    call_me_R 評(píng)論0 收藏0
  • leetcode72. Edit Distance

    摘要:題目要求輸入兩個(gè)字符串和,允許對(duì)進(jìn)行插入,刪除和替換的操作,計(jì)算出將轉(zhuǎn)化為所需要的最少的操作數(shù)。其中存儲(chǔ)的是轉(zhuǎn)換為的最小步數(shù)。首先從邊緣情況開始考慮。只要在此基礎(chǔ)上再進(jìn)行一次插入操作即可以完成轉(zhuǎn)換。 題目要求 Given two words word1 and word2, find the minimum number of steps required to convert wor...

    venmos 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

lewinlee

|高級(jí)講師

TA的文章

閱讀更多
最新活動(dòng)
閱讀需要支付1元查看
<