LeetCode: Longest Substring Without Repeating Characters
来源:互联网 发布:中信淘宝信用卡权益 编辑:程序博客网 时间:2024/06/04 01:01
题目:
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 asubstring,"pwke"
is a subsequence and not a substring.
给定一个字符串,要求给出其最长无重复字符的子串的长度
给出"abcabcbb"
, 符合要求的子串是 "abc"
, 长度是3.
给出 "bbbbb"
, 符合要求的子串是"b"
, 长度是1.
给出 "pwwkew"
,符合要求的子串是"wke"
, 长度是3. 注意答案要求的是子串, "pwke"
是一个子字符序列而并非子串.
思路:
假设有字符串a0,a1,a2,...,an,要求得他的最长无重复子串可以从a0开始,若a0,a1,...,ai 各不相同,那a0到ai的最长无重复子串就是他自己,我们把他的长度做一个备份s0,假定ai+1等于a0到ai中的一个元素aj,显然包含ai+1的最长无重复子串为aj+1,...,ai+1.故我们以aj+1为无重复子串的左端点继续向右,直到遍历整个字符串之后s0,s1,s2,...,sk,..中最大的值即为最长无重复字符的子串的长度。
代码
class Solution {public: int lengthOfLongestSubstring(string s) { vector<int> dict(256,-1); int maxlength=0;int start=-1; for(int i=0;i<s.size();i++){ if(dict[s[i]]>start) start=dict[s[i]]; dict[s[i]]=i; maxlength=max(maxlength,i-start); } return maxlength; }};
阅读全文
0 0
- LeetCode: Longest Substring Without Repeating Characters
- LeetCode Longest Substring Without Repeating Characters
- LeetCode: Longest Substring Without Repeating Characters
- [Leetcode] Longest Substring Without Repeating Characters
- leetcode 23: Longest Substring Without Repeating Characters
- [LeetCode] Longest Substring Without Repeating Characters
- LeetCode 3 - Longest Substring Without Repeating Characters
- [LeetCode]Longest Substring Without Repeating Characters
- leetcode Longest Substring Without Repeating Characters
- 【leetcode】 Longest Substring Without Repeating Characters
- LeetCode-Longest Substring Without Repeating Characters
- [LeetCode] Longest Substring Without Repeating Characters
- [LeetCode] Longest Substring Without Repeating Characters
- [leetcode] Longest Substring Without Repeating Characters
- LeetCode - Longest Substring Without Repeating Characters
- LeetCode 3: Longest Substring Without Repeating Characters
- leetcode-3:Longest Substring Without Repeating Characters
- leetcode之Longest Substring Without Repeating Characters
- (排序算法)谈一谈各类算法和数据结构的c++实现以及相关操作的复杂度(一)
- HDU 4027 线段树
- 欢迎使用CSDN-markdown编辑器
- 首次登陆与在线查询
- servlet中根路径和相对路径
- LeetCode: Longest Substring Without Repeating Characters
- POJ1984 Dirt Ratio(详细解析+数据)_并查集+离线操作
- C++ bitset 类的使用与简介
- 设置一个长宽一样的img的css代码
- base64编解码
- 以太网交换机工作原理
- IO的部分方法(2)
- iOS多线程开发之NSOperation
- find()与children(),parent()与parents()理解与运用