【35】3. Longest Substring Without Repeating Characters

3. Longest Substring Without Repeating Characters

  • Total Accepted: 244703
  • Total Submissions: 1029071
  • Difficulty: Medium
  • Contributors: Admin

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.

 1 class Solution {
 2 public:
 3     int lengthOfLongestSubstring(string s) {
 4         int m[256] = {-1};
 5         int left = 0;
 6         //int right = 0;//不需要用到right,往右走的时候直接用i替代即可
 7         int res = 0;
 8         for(int i = 0; i < s.size(); i++){
 9             if(m[s[i]] == 0 || m[s[i]] < left){
10                 res = max(res, i - left + 1);
11             }else{
12                 left = m[s[i]];//更新左边界
13             }
14             m[s[i]] = i + 1;//这样赋值,记录的是下次的最左边界可以到哪里
15         }
16         return res;
17     }
18 };
时间: 2024-08-17 14:46:42

【35】3. Longest Substring Without Repeating Characters的相关文章

【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

【LeetCode】003 Longest Substring Without Repeating Characters

题目:LeetCode 003 Longest Substring Without Repeating Characters 题意:给一个字符串,找到一个没有重复字符的最长子串. 样例:”bbbbb” 返回”b”的长度1:”abcabcbb”返回”abc”的长度3. 思路: 动态规划.dp[i]表示以第i个字符为结尾的无重复字符的子串的最长的长度,需要一个辅助数组idx[s[i]]记录字符当前字符s[i]上一次出现的位置,如果未出现过则为-1.所以得到如下的递推公式: 另外由于不确定字符串长度的

【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刷题系列 - 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

【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

【Leet Code】Longest Substring Without Repeating Characters

Longest Substring Without Repeating Characters Total Accepted: 20506 Total Submissions: 92223My Submissions Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating le

【LeetCode从零单排】No 3 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 su