leetcode3
public class Solution
{
public int LengthOfLongestSubstring(string s)
{
var dic = new Dictionary<char, int>();
var maxLength = ;
for (int i = ; i < s.Length; i++)
{
var c = s[i];
if (!dic.ContainsKey(c))
{
dic.Add(c, i);
}
else
{
i = dic[c];
var curLen = dic.Count;
if (maxLength < curLen)
{
maxLength = curLen;
}
dic.Clear();
}
} var lastLen = dic.Count;
if (maxLength < lastLen)
{
maxLength = lastLen;
}
return maxLength;
}
}
这种解决方案思想是比较好的,但是实际的代码却引入了不必要的开销(如dic.Count的计算,dic.Clear()的处理等问题),因此执行效果不如普通的滑动窗口。
下面给出另一种执行效率更高的写法:
public class Solution
{
public int LengthOfLongestSubstring(string s)
{
int n = s.Length, ans = ;
Dictionary<char, int> map = new Dictionary<char, int>();
for (int j = , i = ; j < n; j++)
{
if (map.ContainsKey(s[j]))
{
//i记录没有出现重复字符的子串的起始索引
i = Math.Max(map[s[j]], i); //map记录每一个字符,当前循环为止的最大的索引+1,(+1是为方便计算)
map[s[j]] = j + ;
}
else
{
//新字符第一次出现,记录其索引+1,(+1是为方便计算)
map.Add(s[j], j + );
}
ans = Math.Max(ans, j - i + );
}
return ans;
}
}
在补充一份python的实现:
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
n = len(s)
if n == :
return
if n == :
return
dic = {}
maxlength =
left =
right =
while left < n:
right = left
while right < n:
if s[right] in dic and dic[s[right]] >= left:
length = right - left
maxlength = max(maxlength,length)
left = dic[s[right]] +
dic[s[right]] = right
right +=
else:
dic[s[right]] = right
right += if right == n:
length = right - left
maxlength = max(maxlength,length)
return maxlength
maxlength = max(maxlength,right-left)
return maxlength
leetcode3的更多相关文章
- Leetcode3:Longest Substring Without Repeating Characters@Python
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- LeetCode3:Longest Substring Without Repeating Characters
题目: Given a string, find the length of the longest substring without repeating characters. For examp ...
- LeetCode----3 Sum
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
- leetcode3:不重复的最长子串长度
package hashmap; import java.util.HashMap; import java.util.Map; public class hashmap { public stati ...
- LeetCode3 Longest Substring Without Repeating Characters
题意: Given a string, find the length of the longest substring without repeating characters. Examples: ...
- leetcode3 Two Sum III – Data structure design
Question: Design and implement a TwoSum class. It should support the following operations: add and f ...
- [Swift]LeetCode3. 无重复字符的最长子串 | Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- leetcode3:无重复字符的最长子串
给定一个字符串,找出不含有重复字符的最长子串的长度. 示例: 给定 "abcabcbb" ,没有重复字符的最长子串是 "abc" ,那么长度就是3. 给定 &q ...
- leetcode-[3]Max Points on a Line
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line 思 ...
随机推荐
- nmon监控与 nmon analyser分析
参考 https://www.cnblogs.com/wnfindbug/p/5719181.html 1.下载 nmon https://zh.osdn.net/projects/sfnet_nmo ...
- HTML5:表格相关标记及其属性
表格相关标记及其属性 <table>:表格,包括以下属性 属性 说明 width 宽度(有像素和百分比两种表示方法) height 高度(有像素和百分比两种表示方法) border 边框粗 ...
- 团队项目(MVP------新能源无线充电管理网站)(总结)
经过了几个月的学习时间与团队的磨合以及一系列的困难之后,我们mvp小组一起完成了这个项目,内心也是十分激动和有成就感的.其实一开始基础并不好,很多都不知道,但是通过在慕课网上的学习以及老师严厉地督促下 ...
- .NET并行计算和并发5:多线程编程一般指导性原则
使用多线程时要考虑以下准则: 不要使用 Thread.Abort 终止其他线程. 对另一个线程调用 Abort 无异于引发该线程的异常,也不知道该线程已处理到哪个位置. 不要使用 Thread.Sus ...
- 如何利用 Git 与 GitHub 进行多人协作开发
方法一:添加 Collaborators Collaborators 类似于Team模式. Repository的拥有者Owner 可以直接添加合作者到自己的仓库中, 让合作者拥有几乎等同拥有者的权限 ...
- linux基础之用户登录信息查看命令
用户登录信息查看命令 1.who命令 同一个账号通过不同终端登录也属于不同的登录信息,这里不同的终端包含虚拟终端和模拟终端,因为一个用户通过一个终端登录属于一个session 基本介绍 打印当前系统上 ...
- Jquery仿百度经验左右滚动切换效果(转)
http://www.xwcms.net/webAnnexImages/fileAnnex/201608/61342/index.html
- findHomography(src_points, dst_points, CV_RANSAC)
Homography,即单应性,该函数用于求src_points转换为dst_poinsts的单应性矩阵: 为了理解单应性,必须先引入透视变换的概念:把空间坐标系中的三维物体或对象转变为二维图像表示的 ...
- 使用miniconda创建python虚拟环境
安装python指定环境 conda create -n oldboy python=3.6.5 安装环境的同时安装相应的包 conda create -n oldboy python=3.6.5 p ...
- excel 表格粘贴到word 显示不完整
左上角,十字点,右键,取消固定宽度即可: