第三题:Longest Substring Without Repeating Characters

题目链接:题目链接

找到最长不重复子串,使用hash方法来做是极好的!遍历string,找到一个字符,然后会判断这个字符是不是已经在当前的这个子串中,这个使用hash方法,因为可见字符最多256个,那么使用hash[256]就可以!初始化为-1,保存的是相应的字符在string中的位置。如果遇到在前面存在的字符,那么需要将这个子串的start位置移动到已经存在的位置后面一位,然后继续遍历,直到遍历结束。

具体看下面的代码:

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        int max_len = 0, start = 0, i = 0, n = s.length(), len = 0;
        int hash_elem[256];

        memset(hash_elem, -1, sizeof(hash_elem));

        while (i < n){
            // hash_elem[s[i]] < 0说明:是新的字符,之前没有出现过
            // hash_elem[s[i]] < start说明:字符出现过,但是已经作废
            // 例如: abcdb
            // 我们遍历到abcd,然后发现b重复,那么后面其实是从c开始,
            // 但是a由于b的影响也就被作废了,但是这个时候a在hash_elem中
            // 的下标还是0,但是小于start,所以相当于也是合法的字符
            //
            if (hash_elem[s[i]] < 0 || hash_elem[s[i]] < start)
                ++len;
            else{
                max_len = max_len > len ? max_len : len;
                // 注意吧前面的字符减掉
                //
                len -= hash_elem[s[i]] - start;
                // 新的位置从重复字符的位置后面一位开始
                //
                start = hash_elem[s[i]] + 1;
            }

            hash_elem[s[i]] = i;
            ++i;
        }

        return max_len > len ? max_len : len;
    }
};
时间: 2024-11-06 07:08:53

第三题:Longest Substring Without Repeating Characters的相关文章

LeetCode 第 3 题(Longest Substring Without Repeating Characters)

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 "b

Leetcode第三题_Longest Substring Without Repeating Characters

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

leetcode_3题——Longest Substring Without Repeating Characters(set,哈希表,两个指针)

Longest Substring Without Repeating Characters Total Accepted: 62719 Total Submissions: 298285My Submissions Question Solution Given a string, find the length of the longest substring without repeating characters. For example, the longest substring w

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 s

周刷题第二期总结(Longest Substring Without Repeating Characters and Median of Two Sorted Arrays)

这周前面刷题倒是蛮开心,后面出了很多别的事情和问题就去忙其他的,结果又只完成了最低目标. Lonest 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 t

【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

LeetCode第[3]题(Java):Longest Substring Without Repeating Characters 标签:Linked List

题目中文:没有重复字符的最长子串 题目难度:Medium 题目内容: 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 &q

刷题3. Longest Substring Without Repeating Characters

一.题目 Longest Substring Without Repeating Characters,具体请自行搜索. 这个题目,我看了一下,经过一番思考,我觉得实现起来不是很复杂. 但要做到bug free有点难度,主要是边界的问题. 二.这个题目,我自己实现,没有参考代码 提交了5次: 第1次: Wrong Answer,主要是" "这个空串不对 第2次.第3次:Runtime Error,"au" out of Range 第4次:Wrong Answer,

【LeetCode每天一题】Longest Substring Without Repeating Characters(最长无重复的字串)

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 l

【LeetCode刷题系列 - 003题】Longest Substring Without Repeating Characters

题目: 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 3. Example 2: Input: "bbbbb" Output: 1 Exp