LeetCode 3. 无重复字符的最长子串
2019-07-02 17:52
357 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/MordernLi/article/details/94462157
LeetCode 3. 无重复字符的最长子串
给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。
示例 1:
输入: “abcabcbb”
输出: 3
解释: 因为无重复字符的最长子串是 “abc”,所以其长度为 3。
示例 2:
输入: “bbbbb”
输出: 1
解释: 因为无重复字符的最长子串是 “b”,所以其长度为 1。
示例 3:
输入: “pwwkew”
输出: 3
解释: 因为无重复字符的最长子串是 “wke”,所以其长度为 3。
请注意,你的答案必须是 子串 的长度,“pwke” 是一个子序列,不是子串。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters
class Solution { public: int lengthOfLongestSubstring(string s) { int len=0;//计算子串长度 int x,y;//记录两个相同字符的位置 x=0,y=1; while(y<=s.size()) { for(int i=x;i<y;i++) { if(s[y]==s[i]) { (len>y-x)?:len=(y-x); x=i+1; y=x+1; break; } else if(y-1==i) { (len>y-x)?:len=(y-x); y++; break; } } } return len; } };
执行用时 :416 ms, 在所有 C++ 提交中击败了7.95%的用户
内存消耗 :9.2 MB, 在所有 C++ 提交中击败了87.59%的用户
执行效果不理想,时间复杂度较高,可以尝试使用哈希表进行解决
相关文章推荐
- Leetcode:3.无重复字符的最长子串
- LeetCode-3.无重复字符的最长子串
- LeetCode3. 无重复字符的最长子串(Java)
- Leetcode——3. 无重复字符的最长子串
- leetcode 3. 无重复字符的最长子串(Java 版)
- leetcode 3. 无重复字符的最长子串
- 【Leetcode一刷】3.无重复字符的最长子串
- leetcode 3. 无重复字符的最长子串
- 【leetcode】3. 无重复字符的最长子串
- 【Leetcode】3. 无重复字符的最长子串[未解出]
- LeetCode 3. 无重复字符的最长子串
- 一天一道LeetCode(二)3. 无重复字符的最长子串
- Leetcode3. 无重复字符的最长子串
- LeetCode题解(python)-3.无重复字符的最长子串
- LeetCode3. 无重复字符的最长子串(python)
- LeetCode3. python实现:无重复字符的最长子串问题☆☆
- LeetCode 3. 无重复字符的最长子串
- Leetcode 3.无重复字符的最长子串
- LeetCode 3. 无重复字符的最长子串
- 【LeetCode】3. 无重复字符的最长子串(Longest Substring Without Repeating Characters,24.2%,中等)