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: ...
随机推荐
- html5拖拽总结
拖拽(Drag 和 drop)是 HTML5 标准的组成部分.拖拽是一种常见的特性,即抓取对象以后拖到另一个位置. Internet Explorer 9.Firefox.Opera 12.Chrom ...
- 【IOS 开发】Object - C 入门 之 数据类型详解
1. 数据类型简介及输出() http://www.把下面的替换我.com/html/topnews201408/79/1279.htm csdn123
- android 画虚线、实线,画圆角矩形,一半圆角
1.画虚线,实线: 建立dotted_line_gray.xml文件放在drawable文件夹下面. android:shape="line" 可以修改你想要的形状 <?xm ...
- Rigidbody相关的操作最好放在FixedUpdate中,update中可能会无效果
void Turning() { // Create a ray from the mouse cursor on screen in the direction of the camera. Ray ...
- 可以结合react的ui组件
https://ant.design/components/switch-cn/
- 从github拉取项目到myeclipse本地
1.首先拿到jacky-lulu分享的地址 https://github.com/jacky-lulu1/cxf_client 2.登录jacky-lulu账号,fork一份cxf_client到自己 ...
- AX2012 XppCompiler create method动态创建方法并运行
在用友系列的软件中经常可以看到可配置的计算公式,AX中其实也有可配置的公式,如call center呼叫中心的欺诈Fraud rule的配置,AX后台可以根据配置规则,变量,条件来动态产生方法并执行, ...
- python——创建django项目全攻略(野生程序员到家养程序员的完美进化)
新建工程 我用pycharm写代码,所以一般就用pycharm创建django工程.右上角File-New Project.选择路径,修改项目名称,确定.就可以创建一个新的django工程. ...
- mybatis高级(2)_数据库中的列和实体类不匹配时的两种解决方法_模糊查询_智能标签
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "- ...
- 深入springMVC------文件上传源码解析(上篇)
最近在项目中,使用springmvc 进行上传文件时,出现了一个问题: org.springframework.web.multipart.MultipartException: The curren ...