【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
Explanation: The answer is "b", with the length of 1.

Example 3:

Input: "pwwkew"
Output: 3
Explanation: 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.
 

代码(C++实现):

 1 class Solution {
 2 public:
 3     int lengthOfLongestSubstring(string s)
 4     {
 5         // 定义一个map用来存放整个字符串s
 6         unordered_map<char, int> unmap;
 7
 8         // tempLength记录每次扫描位置开始的一次统计的最长字符串的长度
 9         int tempLength = 0;
10         // 众多tempLength中的最大值
11         int maxLength = 0;
12
13         // 第一层循环:分别以s中的每个字符为基准,进行遍历
14         for (int j = 0; j < s.size(); j++)
15         {
16             // 第二层循环:以当前第一层循环中当前的字符为基准,进行遍历,统计以此字符为基准的tempLength
17             for (int i = j; i < s.size(); i++)
18             {
19                 // 是否tempLength继续增加的条件是,map中没有出现过当前指向的字符
20                 if (unmap.count(s[i]) == 0)
21                 {
22                     pair<char, int> myshopping(s[i], i);
23                     // 如果当前的map中无此字符,将当前字符插入到map中
24                     unmap.insert(myshopping);
25                     tempLength++;
26                     maxLength = maxLength > tempLength ? maxLength : tempLength;
27                 }
28                 // 当前字符已经在map中了,直接break,并将本次使用的map进行清除操作
29                 else
30                 {
31
32                     tempLength = 0;
33                     unmap.clear();
34                     break;
35                 }
36
37             }
38         }
39
40         return maxLength;
41         }
42 };


原文地址:https://www.cnblogs.com/xuelisheng/p/10771848.html

时间: 2024-10-14 00:36:54

【LeetCode刷题系列 - 003题】Longest Substring Without Repeating Characters的相关文章

周刷题第二期总结(Longest Substring Without Repeating Characters and Median of Two Sorted Arrays)

这周前面刷题倒是蛮开心,后面出了很多别的事情和问题就去忙其他的,结果又只完成了最低目标. Lonest 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 t

【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 第三题,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 s

LeetCode 第 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 "b

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

刷题3. Longest Substring Without Repeating Characters

一.题目 Longest Substring Without Repeating Characters,具体请自行搜索. 这个题目,我看了一下,经过一番思考,我觉得实现起来不是很复杂. 但要做到bug free有点难度,主要是边界的问题. 二.这个题目,我自己实现,没有参考代码 提交了5次: 第1次: Wrong Answer,主要是" "这个空串不对 第2次.第3次:Runtime Error,"au" out of Range 第4次:Wrong Answer,

【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.所以得到如下的递推公式: 另外由于不确定字符串长度的