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: ...
随机推荐
- 图像预处理第9步:存为.bmp文件
//图像预处理第9步:将最终标准化后的字符图像分为单个单个的HDIB保存,并存为.bmp文件 void CChildView::OnImgprcToDibAndSave() { unsigned ch ...
- what is difference in (int)a,(int&)a,&a,int(&a) ?
This interview question come from a famous communication firm of china. : ) #include <iostream> ...
- Javascript 数组常用操作方法
一.数组 Array 1.创建数组 /* 构造函数 */ var arr1 = new Array(); //创建一个空数组 var arr1 = new Array(5); //创建指定长度数组(数 ...
- 个人psp
排球计分程序 1.计划 通过对用户故事估计这个任务需要3~5d天. 2.开发 2.1需求分析 作为一个观众,我希望了解每场比赛的比分,以便了解比赛的情况. 作为一个观众,我希望输入球队名称查询球队比分 ...
- C#操作SQL Server数据库
http://www.cnblogs.com/rainman/archive/2012/03/13/2393975.html
- Session的使用过程中应注意的一个小问题
在学习AllEmpty大神的从零开始编写自己的C#框架系列文章中,发现的问题:在验证码的缓存Session["vcode"]的赋值时,发现Session["vcode&q ...
- python3 生成器&迭代器
#Author by Andy#_*_ coding:utf-8 _*_import timefrom collections import Iterable#列表生成式def func(): lis ...
- Sql获取周、月、年的首尾时间。
,) -- 本周周一 ,,,)) -- 本周周末 ,) -- 本月月初 ,,,)) -- 本月月末 ,,) -- 上月月初 ,,)) -- 上月月末 ,) -- 本年年初 ,,,)) -- 本年年末 ...
- underscore源码解析 (转载)
转载出自http://www.cnblogs.com/human/p/3273616.html (function() { // 创建一个全局对象, 在浏览器中表示为window对象, 在Node.j ...
- asp.net Lodop实现批量打印
1.列表(前台) <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="w_stu ...