Longest Substring Without Repeating Characters
来源:互联网 发布:淘宝88元xbox360手柄 编辑:程序博客网 时间:2024/04/28 06:02
题目:
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.
分析:遍历字符串,设置一个起始点,一个终点,若碰到重复字符,则改变起始点。算法最坏情况下的时间复杂度为O(n^2),当然,若利用hash查找时间复杂度可降至O(n)
代码如下:
int search(string s,int start,int end)
{
for(int i=end-1;i>=start;i--)
{
if(s[i]==s[end])return (i+1);
}
return start;
}
int lengthOfLongestSubstring(string s)
{
int n=s.length();
if(n<=1)return n;
int start=0,max=1;
for(int i=1;i<n;i++)
{
start=search(s,start,i);
if((i-start+1)>max)
{
max=i-start+1;
}
}
return max;
}
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- 制作jffs2根文件系统
- VC中操作excel表格
- 我安装Opencv时的问题和解决方法
- NAT的完全分析及其UDP穿透的完全解决方案
- HDU1213
- Longest Substring Without Repeating Characters
- FreeMarker教程
- CF 18D Seller Bob
- javascript保留两位小数
- Android入门第八篇之GridView(九宫图)
- 高效程序员的45个习惯1-15
- motor的使用
- 图片颜色空间转换
- 类别,属性,成员变量,arc学习总结