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.

Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.

题目很通俗易懂,就是找一个字符串中,没有重复字符的最长子串的长度。说实话,这道中等难度的题目卡了我很久,最后把它想明白时,内心还是有些小小的激动的。其实不仅是刷题,人生中很多其他方面的事情也都需要有一个积极的心态,不断给自己积极的心理暗示,这样结果一般真的就能如你所愿。

回归正题,这道题暴力搜索法复杂度是O(n^3),这也很好理解,两个指针分别指向字符串的首尾,两重循环,每次都需要判断两个指针之间是否有重复的字符,我们在面试时可以第一时间向面试官讲出这样的思路,至少可以从侧面展现出我们思维敏捷的特点,后续的改进再聊。刷题时,这个复杂度肯定会超时,我们需要进一步改进它。我这里直接把最优解的代码贴出来,时间复杂度为O(n)之后我再解释为何这样做,代码如下:

 1 class Solution {
 2 public:
 3     int lengthOfLongestSubstring(string s) {
 4         //这道题目还是很不错的,参考着优秀解答写一般,O(n)复杂度,以空间换时间。
 5         vector<int> mymap(256,-1);
 6         int i,last=0,ans=0;
 7         for (int i = 0; i < s.length(); i++)
 8         {
 9             if (mymap[s[i]] == -1 || mymap[s[i]] < last)  //如果该字符没有出现过
10                 ans = max(ans, i - last + 1);
11             else
12                 last = mymap[s[i]] + 1;
13             mymap[s[i]] = i;
14         }
15         return ans;
16
17     }
18 };

解释一下思路:last表示字符串开始的位置,

时间: 2024-10-05 04:32:05

LeetCode 3. Longest Substring Without Repeating Characters(medium难度)【精】的相关文章

[LeetCode] 003. Longest Substring Without Repeating Characters (Medium) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 003.Longest_Substring_Without_Repeating_Characters (Medium) 链接: 题目:https://oj.leetcode.com/problems/Longest-Substring-Without-Repeating-Characters/ 代码(github)

【leetcode】 Longest Substring Without Repeating Characters

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

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

[LeetCode][JavaScript]Longest Substring Without Repeating Characters

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 lengt

Leetcode 3 Longest Substring Without Repeating Characters. (最长无重复字符子串) (滑动窗口, 双指针)

目录 问题描述 例子 方法 Leetcode 3 问题描述 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: &q

[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 "bbbbb", the answer is "b", with the length of 1.