LeetCode(3)Longest Substring Without Repeating Characters
来源:互联网 发布:网络电影需要什么手续 编辑:程序博客网 时间:2024/06/06 06:30
题目:
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.
提示标签: hashtable 、 Two Pointers 、 String
分析:
该题目是一个求最长连续子串的长度问题,根据提示标签,题解应该涉及到哈希表的应用,严格控制每个字符的出现下标,以及更新子串位置同时对比当前最长子串的位置。
算法:
class Solution {public: int lengthOfLongestSubstring(string s) { if(s.empty())return 0;int hash_tab[256]; //保存当前字符上一次出现的下标memset(hash_tab , -1 , sizeof(hash_tab)); //hash_tab中初始化所有值为-1int max_len = 0, pos = -1;//max_len即是最长子串长度 , pos为当前子串的开始位置for(int i=0 ; i<s.length() ; i++){//每个字符的元位置初始化为-1 ,当当前字符是重复字符时,改变子串的开始位置if(hash_tab[s[i]] > pos){pos = hash_tab[s[i]];}//ifif(i-pos > max_len){max_len = i-pos;}//if//更改首次出现字符的位置hash_tab[s[i]] = i;}//forreturn max_len; }};
0 0
- LeetCode 3 - Longest Substring Without Repeating Characters
- LeetCode 3: Longest Substring Without Repeating Characters
- leetcode-3:Longest Substring Without Repeating Characters
- LeetCode(3) Longest Substring Without Repeating Characters
- leetcode 3LONGEST SUBSTRING WITHOUT REPEATING CHARACTERS
- Leetcode【3】:Longest Substring Without Repeating Characters
- [leetcode 3] Longest Substring Without Repeating Characters
- [Leetcode] 3 - Longest Substring Without Repeating Characters
- LeetCode 3:《Longest Substring Without Repeating Characters》
- [leetcode 3] Longest Substring Without Repeating Characters
- [leetcode] 3 Longest Substring Without Repeating Characters
- [Leetcode]3Longest Substring Without Repeating Characters
- leetcode 3 Longest Substring Without Repeating Characters
- LeetCode #3 Longest Substring Without Repeating Characters
- leetcode-3 Longest Substring Without Repeating Characters
- LeetCode 3 Longest Substring Without Repeating Characters
- leetcode 3: Longest Substring Without Repeating Characters
- LeetCode 3 Longest Substring Without Repeating Characters
- Java与C/C++网络通信时的数据转换
- SpringMVC工作原理
- Leetcode Construct Binary Tree from Inorder and Postorder Traversal 中序后序遍历重组二叉树
- BitConverter 类
- [转载] 我输给了一份30字的简历
- LeetCode(3)Longest Substring Without Repeating Characters
- MyEclipse打开js文件乱码
- TCP是如何保证包的顺序传输?
- 遮挡描边
- scrollView-保持单方向的bounce
- 追求代码质量: JUnit 4 与 TestNG 的对比
- bzoj 4052: [Cerc2013]Magical GCD
- CG_INLINE,inline 内联函数
- 欢迎使用CSDN-markdown编辑器