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#)的更多相关文章

  1. LeetCode[3] Longest Substring Without Repeating Characters

    题目描述 Given a string, find the length of the longest substring without repeating characters. For exam ...

  2. [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  3. Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

  4. 3. Longest Substring Without Repeating Characters(c++) 15ms

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

  5. 【leetcode】Longest Substring Without Repeating Characters

    题目描述: Given a string, find the length of the longest substring without repeating characters. For exa ...

  6. Longest Substring Without Repeating Characters(C语言实现)

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

  7. leetcode: longest substring without repeating characters

    July 16, 2015 Problem statement: Longest Substring Without Repeating Characters Read the blog: http: ...

  8. [LeetCode_3] Longest Substring Without Repeating Characters

    LeetCode: 3. Longest Substring Without Repeating Characters class Solution { public: int lengthOfLon ...

  9. Longest Substring Without Repeating Characters(Difficulty: Medium)

    题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...

随机推荐

  1. 转 unity 优化

    最近研究U3D开发,个人认为,精通一种新的技术,最快最好的方法就是看它的document,而且个人习惯不喜欢看中文的资料,原汁原味的东西是最正确的,一翻译过来很多东西就都不那么准确了.于是通读了uni ...

  2. TinyOS 中的 task

    task 的目的 做过界面的编程的同学可能会有这种经历,界面不响应,那,其实程序总是在后台运行,但是后台可能是个for循环,那么界面的点击等事件都不能执行. 在windows界面编程中利用了事件机制来 ...

  3. pycharm快捷键、常用设置、包管理

    pycharm快捷键.常用设置.包管理 在PyCharm安装目录 /opt/pycharm-3.4.1/help目录下可以找到ReferenceCard.pdf快捷键英文版说明 or 打开pychar ...

  4. Android6.0之来电转接号码显示修改

    Android6.0之来电转接号码显示修改 手机来电转接界面是在,点开Dialer-搜索框右边的三个点-设置-通话账户(要先插卡)-中国移动或者中国联通--来电转接--第一行,显示始终转接 将所有来电 ...

  5. VB用windows API激活子窗体

    http://files.cnblogs.com/files/liuzhaoyzz/%E6%BF%80%E6%B4%BB%E5%AD%90%E7%AA%97%E4%BD%93.rar setforeg ...

  6. yum源安装Mysql

    Mysql版本:5.7 进入mysql官网,复制下载链接 步骤: (1) wget  http://dev.mysql.com/get/mysql57-community-release-el6-9. ...

  7. for循环每次展示固定条数的数据的写法。

    第一种是一组json数据实现分页的效果,也就是每一次展示5条数据.第二种场景是每一秒展示4条数据,直到全部显示完.两种思路都做个笔记记录下. var nums = 5; //每页出现的数量 var p ...

  8. Javascript学习笔记2.2 Javascript与DOM选项卡(滑动门)案例详解

    学习了DOM的知识,今天开始做些练习,想到了一个网页滑动门的特效,见下图: 1.通过建立索引实现 <!doctype html> <html> <head> < ...

  9. Linux内核分析课程总结

    Linux内核分析课程总结 By 20135203齐岳 知识梳理 (思维导图地址http://mindmap.4ye.me/mkxM0cFh/1) 从start _ kernel构造一个新的Linux ...

  10. 获取下拉框的文本值和value值

    http://www.cnblogs.com/djgs/p/3691979.html?utm_source=tuicool&utm_medium=referral 现在有一个Id为AreaId ...