leetcode4 ---Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without
repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.

题目解析:本题目的含义是在一个字符串中查找不重复的最大子串的问题,返回最大子串的长度。比如“abcabcbb”,最大非重复子串为“abc”,长度为3.“bbbbbb”,最大子串为"b",长度为1.

思路:对于寻找子串的的问题通过枚举遍历所有子串,然后找出最大的子串,该方法的复杂度在N的三次方左右。一直在思考有没有其他可行的方法,对于元素查找的情况,通常会想到hashtable,因为这种方法能够容易处理重复情况且速度快。由于题目中给出的是字母或者数字,根据参考其他方面的分析,把字符串中每个字母的ASCC码值作为table的index,table的具体元素为bool型,在字符串中,如果当前位置以前未出现过该字母,那么就将该子母所对应的table中的元素设置为TRUE,另外需要一个变量记录最大长度。简单的分析入上,参考论坛代码如下:

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        int n = s.length();
  int i = 0, j = 0; //分别用于控制两个子串的长度
  int maxLen = 0;
  bool exist[256] = { false };
  while (j < n) {
    if (exist[s[j]]) {  //判断该字母是否出现,如果出现
      maxLen = max(maxLen, j-i);//判断当前子串与前一个子串的长度
      while (s[i] != s[j]) { //统计下一个子串
        exist[s[i]] = false;
        ++i;
      }
      ++i;
      ++j;
    } else {
      exist[s[j]] = true;
      ++j;
    }
  }
  maxLen = max(maxLen, n-i);
  return maxLen;
    }
};
时间: 2025-01-02 17:05:54

leetcode4 ---Longest Substring Without Repeating Characters的相关文章

LeetCode: Longest Substring Without Repeating Characters 题解

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest subst

LeetCode3 Longest Substring Without Repeating Characters

题目: Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest s

[LeetCode] 3. Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which the length is 3. Given "bbbbb", the answer is "b", with the length of 1.

LeetCode Problem 3.Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which the length is 3. Given "bbbbb", the answer is "b", with the length of 1.

Leetcode 03 Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest subst

Java for LeetCode 003 Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest subst

LeetCode Longest Substring Without Repeating Characters 最长不重复子串

题意:给一字符串,求一个子串的长度,该子串满足所有字符都不重复.字符可能包含标点之类的,不仅仅是字母.按ASCII码算,就有2^8=128个. 思路:从左到右扫每个字符,判断该字符距离上一次出现的距离是多少,若大于max,则更新max.若小于,则不更新.每扫到一个字符就需要更新他的出现位置了.这里边还有个注意点,举例说明: 假如有长为16串 s="arbtbqwecpoiuyca" 当扫到第2个b时,距离上一个b的距离是2:(直接减) 当扫到第2个c时,距离上一个c的距离是6:(直接减

LeetCode No.3 Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest subst

[Leetcode] Longest Substring Without Repeating Characters (C++)

题目: Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest s