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. Nodejs学习总结 -Express 登录注册示例(二)

    项目创建后,我们来做个登录注册实例,详细操作步骤如下. 1.新建项目demo ,具体操作步骤参考上一章内容 https://www.cnblogs.com/Anlycp/ 2.添加mysql和sess ...

  2. css3箭头效果

    css3 record1 尝试用css写了个箭头效果 思路就是通过span和span子元素i分别通过设置他们的伪元素构造两个箭头,但是i构造的箭头两条线height都是0,hover的时候渐近的动画效 ...

  3. ThinkPHP3.2中if判断条件是两个变量

    <select name="typeId"> <foreach name="typeInfo" item="v"> ...

  4. webpack和gulp的区别

    Gulp应该和Grunt比较,他们的区别我就不说了,说说用处吧.Gulp / Grunt 是一种工具,能够优化前端工作流程.比如自动刷新页面.combo.压缩css.js.编译less等等.简单来说, ...

  5. 从resfful API设计到加密算法

    众所周知,SOAP 是基于XML的webservice协议,传的数据都是xml格式的,而当下resftul设计比较火,因为快效率高,但是安全性就不及SOAP, SOAP定义了xml-security的 ...

  6. Javascript优化后的加减乘除(解决js浮点数计算bug)

    function add(a, b) { var c, d, e; try { c = a.toString().split(".")[1].length; } catch (f) ...

  7. spring框架学习(四)自动装配

    set注入和构造注入有时在做配置时比较麻烦.所以框架为了提高开发效率,提供自动装配功能,简化配置.spring框架式默认不支持自动装配的,要想使用自动装配需要修改spring配置文件中<bean ...

  8. NSURLErrorDomain -999 "Canceled" 错误探究

    完整错误描述为 Error Domain=NSURLErrorDomain Code=-999 "Canceled/已取消" 这个错误一般用来描述某个网络请求在还未被发出时就被意外 ...

  9. org.springframework.jdbc.UncategorizedSQLException异常

    用到了mysql的TRUNCATE函数,这个关键字对spring事务有影响,最后换成了ROUND函数

  10. C++对于大型图片的加载缩放尝试

    Qt对于图片的操作主要集中在这几个类 QImage ,QImageReader ,QPixmap 其中QImage这个类对图片的缩放有几个很不错的技巧,不过对于大图片却并不好使,当我们去看QImage ...