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

資訊專(zhuān)欄INFORMATION COLUMN

[LintCode] strStr [KMP & brute force]

Donald / 1904人閱讀

Problem

For a given source string and a target string, you should output the first index(from 0) of target string in source string.

If target does not exist in source, just return -1.

Note

我終于找到了比較好的KMP算法。
http://alice-alicesspace.blogspot.com/2015/07/strstr-kmp-solution-java.html

Solution
class Solution {
    public int strStr(String source, String target) {
        //write your code here
        if (source == null | target == null) {
            return -1;
        }
        int i, j;
        for (i = 0; i < source.length() - target.length() +1; i++) {
            for (j = 0; j < target.length(); j++) {
                if (source.charAt(i+j) != target.charAt(j)) {
                break;
                }
        }
        if (j == target.length()) {
            return i;
        }
        }
        return -1;
        }
}

KMP算法不如也貼出來(lái)。

class Solution {
    public int strStr(String source, String target) {
        //write your code here
        if (source == null || target == null) {
            return -1;
        }
        if (target.length() == 0) {
            return 0;
        }
        
        int[] prefix = new int[target.length()];
        int k = 0;
        for (int i = 1; i < target.length(); i++) {
            while (k > 1 && target.charAt(k) != target.charAt(i)) {
                k = prefix[k-1];
            }
            if (target.charAt(i) == target.charAt(k)) k++;
            prefix[i] = k;
        }
        k = 0;
        for (int i = 0; i < source.length(); i++) {
            while (k > 1 && source.charAt(i) != target.charAt(k)) {
                k = prefix[k-1];
            }
            if (target.charAt(k) == source.charAt(i)) {
                k++;
            }
            if (k == target.length()) {
                return i - k + 1;
            }
        }
        return -1;
    }
}

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

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

相關(guān)文章

  • [LintCode/LeetCode] Two Sum

    摘要:就不說(shuō)了,使用的解法思路如下建立,對(duì)應(yīng)該元素的值與之差,對(duì)應(yīng)該元素的。然后,循環(huán),對(duì)每個(gè)元素計(jì)算該值與之差,放入里,。如果中包含等于該元素值的值,那么說(shuō)明這個(gè)元素正是中包含的對(duì)應(yīng)的差值。返回二元數(shù)組,即為兩個(gè)所求加數(shù)的序列。 Problem Given an array of integers, find two numbers such that they add up to a s...

    xiaoxiaozi 評(píng)論0 收藏0
  • 【LC總結(jié)】KMP * Implement Strstr

    摘要:建立長(zhǎng)度與目標(biāo)串相等的模式函數(shù)初始化,為,之后,若不重復(fù),賦,若有重復(fù)段,賦對(duì)應(yīng)的模式函數(shù)值不難,建議死記硬背根據(jù)模式函數(shù)用兩個(gè)指針比較兩個(gè)字符串,當(dāng)目標(biāo)串指針和目標(biāo)串長(zhǎng)度相等時(shí),返回差值。 Implement strStr() Problem Implement strStr(). Returns the index of the first occurrence of needle...

    snowell 評(píng)論0 收藏0
  • [LintCode/LeetCode] Two Strings are Anagrams/Valid

    摘要:建立一個(gè)長(zhǎng)度為的數(shù)組,統(tǒng)計(jì)所有個(gè)字符在出現(xiàn)的次數(shù),然后減去這些字符在中出現(xiàn)的次數(shù)。否則,循環(huán)結(jié)束,說(shuō)明所有字符在和中出現(xiàn)的次數(shù)一致,返回。 Program Write a method anagram(s,t) to decide if two strings are anagrams or not. Example Given s=abcd, t=dcab, return true....

    vslam 評(píng)論0 收藏0
  • [LintCode/LeetCode] Contains Duplicate III

    Problem Given an array of integers, find out whether there are two distinct indices i and j in the array such that the absolute difference between nums[i] and nums[j] is at most t and the absolute dif...

    MageekChiu 評(píng)論0 收藏0
  • DVWA學(xué)習(xí)之Brute Force

    摘要:運(yùn)行結(jié)果片段發(fā)現(xiàn)密碼的返回長(zhǎng)度與其他不同,獲得密碼,爆破成功。源碼分析加入了對(duì)登錄失敗次數(shù)做限制,防止爆破用了更為安全的機(jī)制防御注入 BurpSuite-Intruder筆記 Burp intruder是一個(gè)強(qiáng)大的工具,用于自動(dòng)對(duì)Web應(yīng)用程序自定義的攻擊。它可以用來(lái)自動(dòng)執(zhí)行所有類(lèi)型的任務(wù)您的測(cè)試過(guò)程中可能出現(xiàn)的 模塊說(shuō)明 Target 用于配置目標(biāo)服務(wù)器進(jìn)行攻擊的詳細(xì)信息 Posi...

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

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

0條評(píng)論

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