【问题标题】:Find the length of the longest substring without repeating characters查找最长子串的长度而不重复字符
【发布时间】:2016-08-11 19:35:09
【问题描述】:

所以我试图解决这个问题给定一个字符串,找到最长子字符串的长度而不重复字符。我知道基于 HashMap 的解决方案,但是在子字符串重叠的情况下会失败。这是我的代码。

public static int lengthOfLongestSubstring(String s) {

        Deque<Character> primary = new ArrayDeque<>();
        Deque<Character> secondary = new ArrayDeque<>();

        for (int i = 0; i < s.length() ; i++) {
            char c = s.charAt(i);
            if(primary.contains(c)){
                while(primary.peek() != c){
                    secondary.offerLast(primary.poll());
                }
                secondary.offerFirst(c);
                primary = secondary;
                secondary.clear();
            }else{
                primary.offerFirst(c);
            }
        }
        return primary.size();

    }

这在我做primary = secondary 的那一行失败了,否则我认为我在逻辑上做对了。 为了测试正确性,我使用了字符串dvdf 有人可以帮我理解为什么这不起作用。

【问题讨论】:

    标签: java string


    【解决方案1】:

    我想知道这个:

    primary = secondary;
    secondary.clear();
    

    这是通过引用分配。您将primarysecondary 设置为指向相同的数据并将其清除。这是你的意图吗?

    这个呢:

    public static int lengthOfLongestSubstring(String s) {
    
        Deque<Character> primary = new ArrayDeque<>();
        Deque<Character> secondary = new ArrayDeque<>();
        Deque<Character> longest = new ArrayDeque<>();
    
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (primary.contains(c)) {
                // Store longest
                if (primary.size() > longest.size()) {
                    longest = new ArrayDeque<>(primary);
                }
    
                while (primary.peek() != c) {
                    secondary.offerLast(primary.poll());
                }
                secondary.offerFirst(c);
                primary = secondary;
                secondary = new ArrayDeque<>();  // Altered
            } else {
                primary.offerFirst(c);
            }
        }
    
        // Also check at end of line.
        if (primary.size() > longest.size()) {
            longest = primary;
        }
    
        return longest.size();
    }
    

    输出

    • dvdf => 3
    • dvdfvadv => 4

    编辑

    你的逻辑是对的。我只改了一行。

    编辑

    跟踪最长的。

    【讨论】:

    • 不是`dvdf`vdf中最长的不重复字符串,这意味着长度应该是3。
    • 这适用于dvdf,但我认为即使这样也会遇到错误。不能保证当前字符串是最长的字符串。对吗?
    • 我认为我们必须维护一个列表来跟踪由此形成的最长字符串。
    【解决方案2】:

    可能不是您正在寻找的确切答案。尽量避免在多线程环境中使用 ArrayDeque,因为它不是线程安全的。

    通过这个链接::

    Find longest substring without repeating characters

    这会返回一个字符串。您可以使用 .length() 方法并根据需要找到长度。

    希望对你有帮助。

    【讨论】:

      【解决方案3】:

      你可以试试这个:

      public class LongestSubstring {
          public static void main(String [] args){
              System.out.println(longestSub("abcdefgghij"));
      //prints 7 abcdefg g is repeated character
          }
          public static int longestSub(String s) {
              if(s==null)
                  return 0;
              boolean[] flag = new boolean[256];
      
              int result = 0;
              int start = 0;
              char[] arr = s.toCharArray();
      
              for (int i = 0; i < arr.length; i++) {
                  char current = arr[i];
                  if (flag[current]) {
                      result = Math.max(result, i - start);
                      // the loop update the new start point and reset flag array
      
                      for (int k = start; k < i; k++) {
                          if (arr[k] == current) {
                              start = k + 1;
                              break;
                          }
                          flag[arr[k]] = false;
                      }
                  } else {
                      flag[current] = true;
                  }
              }
      
              result = Math.max(arr.length - start, result);
      
              return result;
          }
      }
      

      【讨论】:

      • 我不是在寻找不同的方法或解决方案。我想了解我的逻辑出了什么问题。
      【解决方案4】:
      /*C++ program to print the largest substring in a string without repetation of character.
      eg. given string :- abcabbabcd
      largest substring possible without repetition of character is abcd.*/
      
      #include<bits/stdc++.h>
      using namespace std;
      int main()
      {
          string str,str1;
          int max =0;
          string finalstr;
          vector<string> str2;
          cin>>str;
          int len = str.length();
          for(int i=0;i<len;i++)
          {
              if(str1.find(str[i]) != std::string::npos)
              {
                  str2.push_back(str1);
                  char p = str[i];
                  str1 = "";
                  i--;
                  while(p!=str[i])
                      i--;
              }
              else
                  str1.append(str,i,1);
          }
          str2.push_back(str1);
      
          for(int i=0;i<str2.size();i++)
          {
              if(max<str2[i].length()){
                  max = str2[i].length();
                  finalstr  = str2[i];
              }
          }
          cout<<finalstr<<endl;
          cout<<finalstr.length()<<endl;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-31
        • 2018-10-18
        • 2020-12-09
        • 1970-01-01
        • 2017-05-22
        • 2012-08-07
        • 2020-07-04
        • 1970-01-01
        相关资源
        最近更新 更多