【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 substring is "b", with the length of 1.

贪心。

方法一:

用一个表记录出现过的字符,设置一个窗口向前推进,遇到重复字符,将窗口开始位置后移一位。

 1 class Solution {
 2 public:
 3     int lengthOfLongestSubstring(string s) {
 4         int n = s.size();
 5         int ans = 0;
 6         bool exist[256];
 7         memset(exist, false, sizeof(exist));
 8         int start = 0, end = 0;
 9         while (end < n && start + ans < n) {
10             if (exist[s[end]]) {
11                 exist[s[start++]] = false;
12             } else {
13                 exist[s[end++]] = true;
14             }
15             ans = max(ans, end - start);
16         }
17         return ans;
18     }
19 };

方法二:

用一个index数组记录每个字符上一次出现的位置,同样是设置窗口并移动,但是遇到重复字符字符时就可以知道重复字符上次出现的位置,将开始位置移到该位置之后。

 1 class Solution {
 2 public:
 3     int lengthOfLongestSubstring(string s) {
 4         int n = s.size();
 5         int ans = 0;
 6         int index[256];
 7         memset(index, -1, sizeof(index));
 8         int start = 0, end = 0;
 9         while (end < n && start + ans < n) {
10             if (index[s[end]] < start) {
11                 index[s[end]] = end;
12                 ++end;
13                 ans = max(ans, end - start);
14             } else {
15                 start = index[s[end]] + 1;
16             }
17         }
18         return ans;
19     }
20 };
时间: 2024-10-25 03:04:21

【Leetcode】Longest Substring Without Repeating Characters的相关文章

【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 (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 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

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] 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. 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每天一题】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