簡體   English   中英

查找最長的 Substring 沒有重復字符 - Java

[英]Find the Longest Substring without Repeating Characters - Java

我試圖解決一個 leetcode 問題,但我的解決方案沒有為其中一個測試用例返回正確的值。 我想為此解決方案實現兩指針技術(如果我使用錯誤的技術,歡迎就解釋正確的方法提出建議)。 詳情如下:

Leetcode 問題標題:

最長的 Substring 無重復字符。

問題:

給定一個字符串,找出最長的 substring 的長度而不重復字符。 (返回整數)

我的解決方案:

    public int lengthOfLongestSubstring(String s) {
        //place holder for me to create a "sub-string"
        String sub = "";
        
        //counter variable to count the characters in the substring
        int count = 0;
        int maxCount = 0;
        
        //my attempt at a TWO POINTER TECHNIQUE
        //pointers - to determine when the loop should break
        int head = 0;
        int tail = s.length();
        
        //this variable was intended to be used with the "indexOf" method, as the "start from" index...
        int index = 0;
        
        while(head < tail){
            //check if the next character in the string was already added to my substring.
            int val = sub.indexOf(s.charAt(head), index);
            
            //we proceed if it is -1 because this means the substring didnt previously contain that character
            if(val == -1){
                //added character to my substring
                sub+= s.charAt(head);
                count++;
                head++;
            } else {
                //reset data to default conditions, while continuing at the "head index" and check the rest of the substring
                count = 0;
                sub =  "";
            }
            //determine what the length of the longest substring.
            maxCount = count > maxCount ? count : maxCount;
        }
        return maxCount;
    }

我通過了測試用例:

> "" = expect 0
> " " = expect 1
> "abcabcbb" = expect 3
> "bbbbb" = expect 1
> "pwwkew" = expect 3
> "aab" = expect 2

我失敗的測試用例:

> "dvdgd" = expect 3, but got 2
> "dvjgdeds" = expect 5, but got 4
> "ddvbgdaeds" = expect 6, but got 4

可能的問題:

我相信問題的發生是因為它使用“v”移動通過索引,然后處理 substring“dg”。 所以我嘗試更改解決方案,但修復該問題導致我所有其他案例返回錯誤,所以我認為應該放棄修復嘗試。

我也嘗試過:

每當在字符串中找到字符時,我還嘗試操縱“indexOf”方法來更改起始索引。 然而,這產生了一個無限循環。

我已經嘗試解決這個問題幾個小時了,我被踩了。 因此,任何幫助將不勝感激。 如果可以,請詳細說明您的解釋,我對 DSA 和編程非常陌生,非常感謝。 如果需要我提供更多信息,請告訴我,我會盡力回答。

好吧,這里是 go:

public static int lengthOfLongestSubstring(String s) {
    //place holder for me to create a "sub-string"
    String sub = "";
    
    //counter variable to count the characters in the substring
    int count = 0;
    int maxCount = 0;
    
    //my attempt at a TWO POINTER TECHNIQUE
    //pointers - to determine when the loop should break
    int head = 0;
    int tail = s.length();
    
    //this variable shows where to start from 
    int index = 0;
    
    while(head < tail){
        //check if the next character in the string was already added to my substring.
        int val = sub.indexOf(s.charAt(head)); //search whole substing
        
        //we proceed if it is -1 because this means the substring didnt previously contain that character
        if(val == -1){
            //added character to my substring
            sub+= s.charAt(head);
            count++;
            head++;
            //determine what the length of the longest substring.
            maxCount = count > maxCount ? count : maxCount;
            System.out.println(sub); //let's see what we got so far
        } else {
            //reset data to default conditions, while continuing at the "head index" and check the rest of the substring
            count = 0;
            sub =  "";
            head=index+1; //begin from the next letter 
            index++; //move
        }
        
        
    }
    return maxCount;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM