leetcode_c++:哈希:Longest Substring Without Repeating Characters(003)
来源:互联网 发布:怎样做淘宝详情页 编辑:程序博客网 时间:2024/05/16 05:17
题目
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given “abcabcbb”, the answer is “abc”, which the length is 3.
Given “bbbbb”, the answer is “b”, with the length of 1.
Given “pwwkew”, the answer is “wke”, with the length of 3. Note that the answer must be a substring, “pwke” is a subsequence and not a substring.
算法
数组hash
O(N)
- 申请数组,记录当前字符出现的最近位置,一遍算过去,更新左边界,利用最边界计算;
const int N = 300;class Solution {public: int lengthOfLongestSubstring(string s) { int maxlen = 0,left = 0; int sz = s.length(); int prev[N]; memset(prev,-1,sizeof(prev)); for(int i=0;i<sz;i++){ if(prev[s[i]]>=left) left=prev[s[i]]+1; prev[s[i]] = i; maxlen = max(maxlen,i-left+1); } return maxlen; }};
0 0
- leetcode_c++:哈希:Longest Substring Without Repeating Characters(003)
- LeetCode 003 Longest Substring Without Repeating Characters
- LeetCode-003 Longest Substring Without Repeating Characters
- 【leetcode】003 Longest Substring Without Repeating Characters
- LeetCode 003 Longest Substring Without Repeating Characters
- LeetCode 003-Longest Substring Without Repeating Characters
- [LeetCode]-003-Longest Substring Without Repeating Characters
- LeetCode #003 Longest Substring Without Repeating Characters
- LeetCode 003 Longest Substring Without Repeating Characters
- 003 Longest Substring Without Repeating Characters [Leetcode]
- 003 - Longest Substring Without Repeating Characters
- [LeetCode]003-Longest Substring Without Repeating Characters
- 003Longest Substring Without Repeating Characters (C)
- LeetCode 003 Longest Substring Without Repeating Characters
- LeetCode 003: Longest Substring Without Repeating Characters
- leetcode-003 Longest Substring Without Repeating Characters
- 003 Longest Substring Without Repeating Characters
- LeetCode-003 Longest Substring Without Repeating Characters
- iOS静态库创建
- C语言基础 用while循环计算10的阶乘
- UnicodeDecodeError: 'utf8' codec can't decode byte 0xc4 in position 0: i报错问题
- apt-get command fail
- Ubuntu两台虚拟机之间Ping不同
- leetcode_c++:哈希:Longest Substring Without Repeating Characters(003)
- 2016年中总结
- C语言基础 for循环嵌套与goto语句配合使用
- SDWebImage 检查某个URL 清空某个URL
- Struts学习体验【坚持更新】
- C语言基础 用do-while循环计算1加到100
- HDOJ 5234 Happy birthday
- 他是埃隆·马斯克的偶像 是爱迪生毕生碾压的对手 一生专利无数却穷困而死
- Mongodb学习-索引