LeetCode *** 3. Longest Substring Without Repeating Characters
2016-04-25 20:07
411 查看
题目:
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given
Given
Given
substring,
分析:
代码:
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.
分析:
代码:
class Solution { public: int lengthOfLongestSubstring(string s) { int start = 0,end, len = s.length(), res = INT_MIN; vector<int> posRes(256, -1); for (end = 0; end<len; ++end) { if (posRes[s[end]] == -1)posRes[s[end]] = end; else if(posRes[s[end]]>=start){ res = max(end - start, res); start = posRes[s[end]] + 1; posRes[s[end]] = end; } else posRes[s[end]] = end; } return max(end-start, res); } };
相关文章推荐
- 符号重载
- mysql基础(二)
- ajax传递给后台数组参数方式
- HDOJ1106
- 获取时间显示
- CentOS7安装vncserver(启动失败及连接黑屏解决办法)
- Educational Codeforces Round 12 A. Buses Between Cities
- I/O系统---流
- CentOS下安装setuptools、pip和virtualenv
- 面试总结
- 2011年北京大学计算机研究生机试真题(题解)
- CentOS 6.5环境使用ansible剧本自动化部署Corosync + pacemaker环境及corosync常用配置详解
- 【南理工oj】116 - 士兵杀敌(二)(线段数 & 树状数组)
- caffe_layer
- Linux 大于2T 解决方法
- 第三章 表,栈和队列
- 程序员的自我修养_之二_曾国藩的“大悔大悟”
- CentOS 6.5环境使用ansible剧本自动化部署Corosync + pacemaker环境及corosync常用配置详解
- LeetCode OJ 110. Balanced Binary Tree
- 心得体会