Longest Substring Without Repeating Characters (c#)
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.
这个问题仅理解他的意思,我就花了很久。。。智商捉急啊
给定一个字符串,求出其中有不重复字符的最长子串。
我一开始的想法是每次遇到有重复出现的字符时将之前的字符串截掉,剩下的进行递归,这样效率非常低下,而且频繁截取字符串,造成TLE
错误解法:
public int LengthOfLongestSubstring(string s) {
string[] ss = new string[s.Length];
int index = ;
int len = ;
for (int i = ; i < s.Length; i++)
{
if (!ss.Contains(s.Substring(i, )))
{
ss[i] = s.Substring(i, );
len++;
}
else
{
for (int j = ; j < ss.Length; j++)
{
if (ss[j] == s.Substring(i, ))
{
index = j;
break;
}
}
s = s.Substring(index + , s.Length - index - );
int temp = LengthOfLongestSubstring(s);
if (temp > len)
{
len = temp;
}
break;
}
}
return len;
}
错误点①s.Substring(i, 1)可以等价于s[i],返回的是char类型
②递归截取剩下的进行下一次是否有重复字符的判断,造成效率低下
优秀解法:
public int LengthOfLongestSubstring(string s) {
if (s.Length == )
{
return ;
}
Hashtable osh = new Hashtable();
int longest = ;
int substringStartPosition = ;
for (int i = ; i < s.Length; i++)
{
if (!osh.ContainsKey(s[i]))
{
osh.Add(s[i], i);
}
else if ((int)osh[s[i]] < substringStartPosition)
{
osh[s[i]] = i;
}
else
{
longest = System.Math.Max(longest, i - substringStartPosition);
substringStartPosition = (int)osh[s[i]] + ;
osh[s[i]] = i;
}
}
return System.Math.Max(s.Length - substringStartPosition, longest);
}
思路:将每个字符和他的位置作为一个key value存储(不一定要用哈希表,只要能判断键值对即可),每当出现重复的字符时,将位置(value)更新;
记录最长的没有重复字符的长度和开始的位置,每次新的判断开始位置为有重复字符出现时的下一个字符位。
这样的好处就是不用频繁截取字符串,只需要记录相应的位置和长度。
Longest Substring Without Repeating Characters (c#)的更多相关文章
- LeetCode[3] Longest Substring Without Repeating Characters
题目描述 Given a string, find the length of the longest substring without repeating characters. For exam ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- 3. Longest Substring Without Repeating Characters(c++) 15ms
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- 【leetcode】Longest Substring Without Repeating Characters
题目描述: Given a string, find the length of the longest substring without repeating characters. For exa ...
- Longest Substring Without Repeating Characters(C语言实现)
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- leetcode: longest substring without repeating characters
July 16, 2015 Problem statement: Longest Substring Without Repeating Characters Read the blog: http: ...
- [LeetCode_3] Longest Substring Without Repeating Characters
LeetCode: 3. Longest Substring Without Repeating Characters class Solution { public: int lengthOfLon ...
- Longest Substring Without Repeating Characters(Difficulty: Medium)
题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...
随机推荐
- [poj1182]食物链(并查集+补集)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 64841 Accepted: 19077 Description ...
- Ubuntu下安装Koala
1.下载koala ,官方网址 目前官方链接的到百度云上的包好像有问题,不能安装,这里分享下 https://yunpan.cn/ckAF4L3TR4kKG (提取码:179a) 2.执行 $ sud ...
- PHP中的逻辑运算符的优先级
在三元运算中(expr1)?(expr2):(expr3); and,or,xor的优先级要小于三元运算符,所以需要添加括号例:如果$a为true,$b为fals,$a and $b?"tr ...
- 更改eclipse的Package Explorer的字体
说一个牛B的不像实力派的东西 — 更改eclipse的Package Explorer的字体1. 打开eclipse目录/Applications/Eclipse.app/Contents/Eclip ...
- C#操作SQL Server数据库
http://www.cnblogs.com/rainman/archive/2012/03/13/2393975.html
- maven中运行hibernate5的一些注意事项
问题1:Could not bind factory to JNDI hibernate.cfg.xml中<sessionFactory> 标签中的name="foo" ...
- Java类中中文问题
一个奇怪问题 java类中要保存一个xml文件到数据库,2种传值方式其中1种不知何故会最终导致解析xml时报错. xml文件内容由StringBuffer定义,其中一段内容如下: sb.append( ...
- 使用Memcache缓存mysql数据库操作的原理和缓存过程浅析
转载自脚本之家 http://www.jb51.net/article/51831.htm 作者:忙碌的松鼠 对于大型网站如facebook,ebay等网站,如果没有Memcache做为中间缓存层, ...
- IO流中SequenceInputStream类
SequenceInputStream类: 不断的读取InputStream流对象,对于使用Enumeration对象的情况,该类将持续读取所有InputStream对象中的内容,直到到达最后一个In ...
- mac下使用sencha cmd+extjs6
笔者刚接手公司一个项目,后台是使用extjs6做前端,php做api接口,两者通过ajax交互 没办法,不管接手的项目多么的挫逼,都还是要上的,拿人钱财替人消灾嘛 首先是安装sencha cmd ,百 ...