[刷题] LeetCode 3 Longest Substring Without Repeating Character

要求

  • 在一个字符串中寻找没有重复字母的最长子串

思路

  • 滑动窗口
  • 如果当前窗口没有重复字母,j右移,直到包含重复字母
  • i右移,直到不包含重复字母
  • 用数组记录字母是否出现过,判断重复

实现

 1 class Solution{
 2 public:
 3     int lenthOfLongestSubstring(string s){
 4         int freq[256] = {0};
 5         int l = 0, r = -1;
 6         int res = 0;
 7
 8         while(l < s.size()){
 9             if( r + 1 < s.size() && freq[s[r+1]] == 0)
10                 freq[s[++r]] ++ ;
11             else
12                 freq[s[l++]] -- ;
13             res = max(res, r-l+1);
14         }
15         return res;
16     }
17 };

相关

  • 438 Find All Anagrams in a String
  • 76 Minimum Window Substring

原文地址:https://www.cnblogs.com/cxc1357/p/12596462.html

时间: 2024-11-08 07:19:03

[刷题] LeetCode 3 Longest Substring Without Repeating Character的相关文章

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

 1.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

Leetcode 解题 Longest Substring without repeating charcater python

原题: Given a string, find the length of the longest substring without repeating character For example, the Longest substring without repeating letters for "abcabcbb" is "abc", with the length is 3 思路:参考blog.csdn.net/hcbbt/article/detail

【leetcode】 Longest Substring Without Repeating Characters

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

[LeetCode] 5. Longest Substring Without Repeating Characters 最长回文子串

[LeetCode] 5. Longest Substring Without Repeating Characters Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. Example 1: Input: "babad" Output: "bab" Note: "aba&qu

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

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

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