leetcode_c++:哈希:Longest Substring Without Repeating Characters(003)
2016-06-25 13:39
513 查看
题目
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.
算法
数组hashO(N)
申请数组,记录当前字符出现的最近位置,一遍算过去,更新左边界,利用最边界计算;
const int N = 300; class Solution { public: int lengthOfLongestSubstring(string s) { int maxlen = 0,left = 0; int sz = s.length(); int prev ; 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; } };
相关文章推荐
- 浅析C++多态及其实现模式
- C++11 新特性:显式 override 和 final
- Problem C: C语言习题5.24--文件操作1
- Problem F: C++结构体之统计最高最低分
- leetcode #141 in cpp
- leetcode #140 in cpp
- Problem B: C语言习题 学生成绩输入和输出
- C++设计模式浅识组合模式
- Problem A: C语言习题 计算该日在本年中是第几天
- c++中vector(容器)的用法
- Problem E: C语言习题 输出月份
- Problem D: C语言习题 不等长字符串排序
- C++设计模式浅识备忘录模式
- Problem C: C语言习题 等长字符串排序
- C++设计模式浅识适配器模式
- Problem B: C语言习题 字符串比较
- C++设计模式浅识状态模式
- Problem A: C语言习题 字符串长度
- C++设计模式浅识抽象工厂模式
- leetcode #139 in cpp