繁体   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