LeetCode: Longest Substring Without Repeating Characters
来源:互联网 发布:刷信誉平台源码 编辑:程序博客网 时间:2024/06/06 03:53
思路:时间复杂度是O(n),用hash表,搜索字符串,如果碰到s[i]已经在位置 pre(s[i]) 出现过,那么将当前位置的最长不重复子串设置为 i - pre[s[i]],(假设这个区间之中没有重复出现的字符串,后面的start变量可以保证这一点),设置一个start变量,表示从start前面已经出现了重复的字母,所以后面出来的字母只能从start位置开始统计,每次一碰到重复字母就更新start变量。
class Solution {public: int lengthOfLongestSubstring(string s) {if(s.empty())return 0;unordered_map<int, int> flag;int ret = 0, start = 1, len = s.length(), * f;f = new int[len];for(int i = 0;i < s.length();i++){if(flag[s[i] - 'a']>=start){f[i] = i - flag[s[i] - 'a'] + 1;start = flag[s[i] - 'a'] + 1;}elsef[i] = i - start + 2;flag[s[i] - 'a'] = i + 1;ret = max(ret,f[i]);}return ret; }};
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
- linq list select用法注意事项
- 避免开发恶心App的7个小工具
- 前台将多个json对象传到后台解析
- struts2 文件上传和下载,以及部分源码解析
- cmake 学习笔记(一)
- LeetCode: Longest Substring Without Repeating Characters
- 前端架构
- windows下搭建Android NDK开发环境
- break和continue语句
- Android Intent的几种用法全面总结
- Oracle的优化器的RBO和CBO方式
- 关于委托
- Android 之采用execSQL和rawQuery方法完成数据的添删改查操作
- [转]XMPP协议的原理介绍