3.Longest Substring Without Repeating Characters(string; DP)

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.

思路:使用动态规划,记住当前最长 substring及开始的字符位置,这样时间复杂度最坏O(n2),最好情况下O(n)

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        if(s=="") return 0;

        //状态信息
        int maxLen = 1;
        int curLen = 1;
        int startIdx = 0; 

        int i, j;
        for(i = 1; i < s.length(); i++){
            for(j = startIdx; j < i; j++){
                if(s[j] == s[i]){
                    //更新状态信息
                    if(curLen > maxLen) maxLen = curLen;
                    startIdx = j+1;
                    curLen = i - startIdx + 1;
                    break;
                }
            }
            if(j == i){
                curLen++;//更新状态信息
            }

            if(curLen > maxLen) maxLen = curLen;//更新状态信息
        }
        return maxLen;
    }
};
时间: 2024-08-06 07:54:53

3.Longest Substring Without Repeating Characters(string; DP)的相关文章

Leetcode 3. Longest Substring Without Repeating Characters(string 用法 水题)

3. Longest Substring Without Repeating Characters Medium Given a string, find the length of the longest substring without repeating characters. Example 1: Input: "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of

(DP)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

[string]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

Given a string, find the length of the 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] Longest Substring Without Repeating Characters (LinkedHashSet的妙用)

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】003 Longest Substring Without Repeating Characters

题目:LeetCode 003 Longest Substring Without Repeating Characters 题意:给一个字符串,找到一个没有重复字符的最长子串. 样例:”bbbbb” 返回”b”的长度1:”abcabcbb”返回”abc”的长度3. 思路: 动态规划.dp[i]表示以第i个字符为结尾的无重复字符的子串的最长的长度,需要一个辅助数组idx[s[i]]记录字符当前字符s[i]上一次出现的位置,如果未出现过则为-1.所以得到如下的递推公式: 另外由于不确定字符串长度的

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 subst

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