当前位置: 首页 > 知识库问答 >
问题:

最长子字符串,时间限制超过java

姬弘文
2023-03-14

给定一个字符串,在不重复字符的情况下,找到最长子字符串的长度。例如,“abcabcbb”的不重复字母的最长子字符串是“abc”,其长度为3。对于“bbbbb”,最长的子字符串是“b”,长度为1。

public static int lengthOfLongestSubstring(String s) {
    if (s.length()==0)
        return 0;
    int maxlen = 1;

    HashMap<Character, ArrayList<Integer>> check = new HashMap<Character,ArrayList<Integer>>();
    for (int i = 0; i < s.length(); i++) {
        for (int j = i; j < s.length(); j++) {
            if (!check.containsKey(s.charAt(j))) {
                ArrayList<Integer> value= new ArrayList<>();
                value.add(j);
                check.put(s.charAt(j), value);
            }
            else {
                maxlen = Math.max(j - i, maxlen);
                ArrayList<Integer> temp = check.get(s.charAt(j));
                i=temp.get(temp.size()-1);  
              // get the last index(biggest index) of the key value
                check.clear();
                break;
            }
            if(j==s.length()-1) {
                maxlen = Math.max(j - i + 1, maxlen);
            }

        }
    }
    return maxlen;
  }
}

对于长可重复字符串的最后一次测试,超过了时间限制。不知道如何优化。寻求改进,谢谢

共有1个答案

夏弘义
2023-03-14

这里有一个相当简单的解决方案,它应该比您的解决方案更快:

public static int longestNonRepeating(final String s) {
    final Set<Character> unique = new HashSet<>();
    int max = 0;
    for (int i = 0; i < s.length(); ++i) {
        final char c = s.charAt(i);
        if (!unique.add(c)) {
            for (int j = i - unique.size(); j < i; ++j) {
                if (s.charAt(j) != c) {
                    unique.remove(s.charAt(j));
                } else {
                    break;
                }
            }
        }
        max = Math.max(max, unique.size());
    }
    return max;
}

这是怎么工作的?

我们沿着字符串走,并将字符添加到中。如果我们添加的字符已经包含在set中,那么我们知道在当前子字符串中有一个重复的字符。

a  b  c  a  b  c
0  1  2  3  4  5
^
|
i
a  b  c  a  b  c
0  1  2  3  4  5
   ^
   |
   i
a  b  c  a  b  c
0  1  2  3  4  5
      ^
      |
      i
a  b  c  a  b  c
0  1  2  3  4  5
^        ^
|        |
j        i
a  b  c  a  b  c
0  1  2  3  4  5
   ^        ^
   |        |
   j        i
a  b  c  a  b  c
0  1  2  3  4  5
      ^        ^
      |        |
      j        i
 类似资料:
  • 问题内容: 我正在寻找一种方法来限制php中的字符串,并在字符串过长时在末尾添加…。 问题答案: 您可以使用类似于以下内容的东西:

  • 这就是leetcode问题:给定一个字符串s,在s中找到最长的回文子字符串。您可以假定s的最大长度是1000。我的解决方案是使用一个dp表,其中dp[i][j]=以S[i]开始,以S[j]结束的最长回文子字符串的长度 我想知道为什么我的解决方案的时间限制超过了错误,不应该是O(n^2)吗?

  • 本文向大家介绍jQuery 限制输入字符串长度,包括了jQuery 限制输入字符串长度的使用技巧和注意事项,需要的朋友参考一下 我们后台做程序的时候,比如录入一篇文章,文章会有摘要,我们希望文章的字符长度是我们可以控制的,我们不希望它太长,比如限制只能输入250个字符,下面的代码实现了这种功能。 先来看一下效果图 代码如下: 以上就是本文的全部内容,希望能给大家一个参考,也希望大家多多支持呐喊教程

  • http://articles.leetcode.com/2011/11/lengton-palindromic-substring-part-i.html 我处理这个问题的领域是用java编写代码,使用简单的强力解决方案,然后使用o(n2)方法,没有额外的空间,就像现在这样。http://www.geeksforgeeks.org/lengte-palindromic-substring-set

  • 我收到一条PHP警告,上面说: PHP警告:POST内容长度2290848字节超过第0行未知中2097152字节的限制, 以下是我的配置: 在 /etc/php.ini 在/etc/httpd/conf/httpd中。形态 从上面看,我的帖子最大尺寸应该是2.5米。为什么在2M(2097152字节)的下限触发此警告?

  • 我知道如何使用动态规划来解决 <罢工> 大多数 给定两个字符串的最长公共子串或最长公共子串。然而,对于字符串Y的子串X的最长子序列问题,我很难找到一个解决方案。 查找字符串X的所有子序列并按长度desc排序; 遍历排序的子序列,如果当前子序列是Y的子字符串,则返回子序列。 它可以工作,但运行时间可能会很糟糕。假设X中的所有字符都是唯一的,那么有2^m个子群,其中m是X的长度,我认为检查一个字符串是