【leetcode】Longest Substring Without Repeating Characters
来源:互联网 发布:加强网络管理的通知 编辑:程序博客网 时间:2024/06/05 00:58
参考链接
http://blog.csdn.net/pickless/article/details/9018575题目描述
Longest Substring Without Repeating Characters
Total Accepted: 11501 Total Submissions: 50924My SubmissionsGiven 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.
题目分析
O(n2)的解法是最为直接的:
1.枚举每一个子串;
2.扫描子串,求出满足题意的最长长度。
其实,这里很多工作都是重复的、无用的。
看一个例子:
S="abbdeca"。
t1="abbdeca",t1[1]==t1[2]。
t2="bbdeca",t2[0]==t2[1]。
t3="bdeca",一直扫描到最后。
t4="deca"、t5、t6、t7都同上。
我们在处理t1的时候已经扫描到了s[2],然后处理t3的时候扫描了s[2]到s[6],这两个子串已经扫描完了整个母串。
换言之,能使得子串停止扫描的位置只有两处:1.s[2];2.s[6](结尾)。
对于另一个例子S="aaab",能使子串停止扫描的位置分别是:s[1],s[2],s[3](结尾)。
所以我们可以考虑只扫描母串,直接从母串中取出最长的无重复子串。
对于s[i]:
1.s[i]没有在当前子串中出现过,那么子串的长度加1;
2.s[i]在当前子串中出现过,出现位置的下标为j,那么新子串的起始位置必须大于j,为了使新子串尽可能的长,所以起始位置选为j+1。
总结
想办法去避免重复的工作!!!就是可优化的地方代码示例
推荐学习C++的资料
- 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
- android中listview的一些样式设置
- HTMLParser使用详解
- 网络电子采购系统的一些介绍
- ONE源代码分析——movement4之BusMovement
- android.os.BinderProxy cannot be cast to com.android.server.am.Activit 解决办法
- 【leetcode】Longest Substring Without Repeating Characters
- pjsua帮助手册(中文)
- ios之获取图库图片
- 《读 大 学 ,究 竟 读什 么 》
- 在Centos 5.2上安装Xen并创建windows 2003虚拟机
- 装系统。。。
- Html注意事项
- 利用session完成登录
- 传统局域网管理