[LeetCode]Longest Substring Without Repeating Characters
来源:互联网 发布:第一人称冒险游戏java 编辑:程序博客网 时间:2024/06/12 02:12
思路:
题目要求找出字符串中的最长无重复子串,那么我们必须枚举出所有的无重复子串,然后比较得出最长的那个。这道题的枚举可以在O(n)的复杂度内实现,即扫一遍母串。
对于第i个字符s[i],有两种情况:
1、s[i]在当前子串中没有出现过,那么该子串的长度+1;
2、s[i]在当前子串中出现过,出现的位置下标为j,那么新子串的起始位置必须大于j,为了使新子串尽可能的长,所以起始位置选为j+1。
代码:
class Solution {public: int lengthOfLongestSubstring(string s) { int len=s.size(); int st=-1,max=0; int i; int pos[1000]; memset(pos,-1,sizeof(pos)); for(i=0;i<len;i++) { if(pos[s[i]]>st) { st=pos[s[i]]; } if(i-st>max) max=i-st; pos[s[i]]=i; } return max; }};
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
- HTML中的超链接
- toad导出表的数据
- 陈老师的多校联合2010811c题||09年武汉站||hdu3232
- JMM介绍
- Runtime类
- [LeetCode]Longest Substring Without Repeating Characters
- Labwindows/CVI下调用openCV实现多摄像头切换
- 母函数之Big Event in HDU hdoj 1171
- bnu36905 Nested Segments 离散化+线段树
- Android开发周围wifi热点和属性的获取
- 一些有用的数论知识。(莫比乌斯反演)
- 陈老师的多校联合2009年武汉站D题 ||hdu 3033
- POJ 1113 Wall 二维凸包
- hdu 2092 求整数解是否存在