Oj练习42——T3 Longest Substring Without Repeating Characters

求一个字符串中不含重复字母的最大子串的长度。

【思路】

1.用临时tmp记录长度,遇到重复字母把tmp当前值赋给要返回的length,tmp归零

比较tmp和length,当tmp>length时,更新length。

2.每个字母向前遍历是否有重复字母,用哈希表。

3.反复提交代码不能通过后看了题目tag,知道需要双指针,用一个begin指向重新计数的开始处,i-begin得到当前长度,

每次遇到重复就把begin赋值为哈希表该字母对应的序号的后一个。

注意:哈希表还要更新重复字母的序号,否则每次都和第一次出现过的比,begin赋值不正确。

【my code】

int lengthOfLongestSubstring(string s) {
        int length=s.size();
        int l=0,tmp=0;
        int begin=0,end=0;
        unordered_map<char,int> charmap;
        for(int i=0; i<length; i++){
            if(charmap.find(s[i])!=charmap.end()){
                if(l<tmp)
                    l=tmp;
                tmp=i-begin;//eg:ohomm
                if(begin<charmap[s[i]]+1)//eg:abba
                    begin=charmap[s[i]]+1;
            }
            charmap[s[i]]=i;
        }
        if(l<tmp) l=tmp;
        return max(l,length-begin);
    }

【总结】

一把辛酸泪!

改了无数遍!最后结果还特别差……哭晕。

看到一个新解法,新开一个数组,排名十分靠前,明天细细分析。

【other code】

int lengthOfLongestSubstring(string s) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        memset(canUse, true, sizeof(canUse));

        int count = 0;
        int start = 0;
        int ret = 0;
        for(int i = 0; i < s.size(); i++)
        {
            if (canUse[s[i]])
            {
                canUse[s[i]] = false;
                count++;
            }
            else
            {
                ret = max(ret, count);
                while(true)
                {
                    canUse[s[start]] = true;
                    count--;
                    if (s[start] == s[i])
                        break;
                    start++;
                }
                start++;
                canUse[s[i]] = false;
                count++;
            }
        }

        ret = max(ret, count);

        return ret;
    }
时间: 2024-11-08 15:07:59

Oj练习42——T3 Longest Substring Without Repeating Characters的相关文章

[LeetCode][Python]Longest Substring Without Repeating Characters

# -*- coding: utf8 -*-'''__author__ = '[email protected]'https://oj.leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, find the length of the longest substring without repeating characters.For example, the longest s

Minimum Window Substring &amp;&amp;&amp; Longest Substring Without Repeating Characters 快慢指针,都不会退,用hashmap或者其他结构保证

1 public class Solution { 2 public static int lengthOfLongestSubstring(String s) { 3 4 char[] arr = s.toCharArray(); 5 int pre = 0; 6 7 HashMap<Character, Integer> map = new HashMap<Character, Integer>(); 8 9 for (int i = 0; i < arr.length;

No.3 Longest Substring Without Repeating Characters

Longest Substring Without Repeating Characters Total Accepted: 167158 Total Submissions: 735821 Difficulty: Medium Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is &

Longest Substring Without Repeating Characters leetcode java

题目: 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题解 || Longest Substring Without Repeating Characters (O(n)算法)问题

problem: 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 long

【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

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