leetcode--Longest Substring Without Repeating Characters
来源:互联网 发布:手机版开淘宝店铺 编辑:程序博客网 时间:2024/06/14 13:57
1.问题
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 substring is "b", with the length of 1.
2.分析
用sub保存当前最大的没有重复的子字符串。取当前字符时,只有两种可能:(1)跟sub字符串前面某个字符重复,那么就要把sub字符串的第0位开始到第一个重复字符之间的所有字符删掉,再把重复字符加到sub后面;(2)跟sub字符串所有字符不重复,当前字符加到sub后面。
贪心策略是,sub子字符串的当前长度大于最大子长度的话,更新最大子长度。
3.
class Solution {public: int lengthOfLongestSubstring(string s) { int maxLen=0,curLen=0; string sub = ""; for(auto c:s) { int postion = sub.find(c); if(postion!=sub.npos) { sub.erase(0,postion+1); curLen = curLen - (postion+1); } sub += c; curLen++; maxLen = max(maxLen,curLen); } return maxLen; }};
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
- 【郑轻】[1798]戴姆勒的求助
- 解决Mac下emacs中alt无效问题
- leetcode:20 Valid Parentheses-每日编程第三十七题
- 二叉树的三种基本遍历
- 消息中间件——基础介绍
- leetcode--Longest Substring Without Repeating Characters
- HTML和CSS的学习笔记(二)
- oracle 导入导出
- 浅析ListView用法
- Android地图应用开发(一)开发前需做的准备
- uva321
- SharedPreferences 首选项浅析(一)
- 输入两个正整数m和n,求其最大公约数和最小公倍数
- 无法使用虚拟目录密码作为用户在本地登录到网站