【leetcode】Longest Substring Without Repeating Characters (middle)

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.

思路:

关键是记录当前子串的起始位置和用hash表记录每个字母出现的情况。

大神简约版的代码:

class Solution {
public:
    int v[256]; //asciib码最多256个
    int lengthOfLongestSubstring(string s) {
        memset(v,-1,sizeof(v)); //位置先全部赋值为-1
        int start = 0, ans = 0;
        for (int i = 0; i < s.size(); ++i) {
            if (v[s[i]] >= start) { //如果当前子串中已经出现了该字符 更新答案和起始位置
                ans = ans > i - start ? ans : i - start;
                start = v[s[i]] + 1;
            }
            v[s[i]] = i;
        }
        ans = ans > s.size() - start ? ans : s.size() - start;
        return ans;
    }
};

思路是一样的,我自己好长好慢的代码:

int lengthOfLongestSubstring(string s) {
        unordered_map<char, int> v;
        int ans = 0;
        int len = 0;
        int start = 0; //记录当前子串的起始位置
        for(int i = 0; i < s.size(); i++)
        {
            if(v.find(s[i]) == v.end())
            {
                len++;
                v[s[i]] = i;
            }
            else
            {
                string tmp = s.substr(i - len, len);
                ans = (len > ans) ? len : ans;
                len = i - v[s[i]];
                for(int j = start; j < v[s[i]]; j++) //把重复字符前面的字符次数都置0
                {
                    v.erase(s[j]);
                }
                start = v[s[i]] + 1;
                v[s[i]] = i;

            }
        }
        ans = (len > ans) ? len : ans;
        return ans;
    }
时间: 2024-12-29 11:41:05

【leetcode】Longest Substring Without Repeating Characters (middle)的相关文章

【leetcode】 Longest Substring Without Repeating Characters

题目: 给定一个字符串,返回该串没有重复字符的最长子串. 分析: 1)子串:子串要求是连续的. 2)无重复,出现重复就断了,必须从新的位置开始.而新的位置就是重复字符第一次出现位置的下一个位置. 3)整个串可能没有一处重复. 那么,为了找出当前访问的字符是否出现过,要怎么做呢?当然是hash,O(1)的时间,而且既然是字符, 定义个255的hash table 就可以了,hash table 中的元素为相应字符在s中出现的位置.初始化为-1,表示都没有出现. 我们另外定义一个start 和end

【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

【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. Longest Substring Without Repeating Characters(python)

要判断最后一个不重复的子串是不是最长 class Solution(object): def lengthOfLongestSubstring(self, s): """ :type s: str :rtype: int """ if len(s)<=1: return len(s) point=0 maxl=0 for index in range(1,len(s)): if s[index] in s[point:index]: l=i

【leetcode】Pascal&#39;s Triangle I &amp; II (middle)

Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 思路:杨辉三角,直接按规律生成即可 vector<vector<int> > generate(int numRows) { vector<vector<int>>

LeetCode 3. Longest Substring Without Repeating Characters(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 "b", with the length of 1.

leetcode 3. Longest Substring Without Repeating Characters (Python版)

题目: 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】Binary Tree Right Side View(middle)

Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom. For example:Given the following binary tree, 1 <--- / 2 3 <--- \ 5 4 <--- You should return [1, 3, 4].

【leetcode】Bitwise AND of Numbers Range(middle)

Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive. For example, given the range [5, 7], you should return 4. 思路: 先找前面二进制相同的位,后面不相同的位相与一定为0. 比如: 1111001 1110011 从0011 - 1001 中间一定会经