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 思 ...
随机推荐
- Jsの练习-数组常用方法 -slice()
slice() 返回从原数组指定开始下标到结束下标之间的项组成的新数组. slice()方法可以接收一个或两个参数,即要返回项的起始和结束位置. 在只有一个参数的情况下,slice()方法返回从该参数 ...
- centos打开matlab的正确姿势
进入usr/local/MATLAB/R2015b/bin ./matlab
- Problem F: 平面上的点——Point类 (VI)
Description 在数学上,平面直角坐标系上的点用X轴和Y轴上的两个坐标值唯一确定.现在我们封装一个“Point类”来实现平面上的点的操作. 根据“append.cc”,完成Point类的构造方 ...
- SharePoint REST API - 一个请求批量操作
博客地址:http://blog.csdn.net/FoxDave 本篇主要讲解如何应用$batch查询选项来批量执行REST/OData请求,它将多个操作捆绑到一个请求中,可以改进应用程序的性能 ...
- HDU5616 天平能否称出物体重量问题 01背包变形或者折半搜索
//hdu5616 void solve1(){dp[0]=1;for(int i=1;i<=n;i++){for(int j=INF;j>=val[i];j--){dp[j]|=(dp[ ...
- Alpha冲刺6
前言 队名:拖鞋旅游队 组长博客:https://www.cnblogs.com/Sulumer/p/10004107.html 作业博客:https://edu.cnblogs.com/campus ...
- makeObjectsPerformSelector对数组中的对象发送消息执行对象中方法
- (void)makeObjectsPerformSelector:(SEL)aSelector; - (void)makeObjectsPerformSelector:(SEL)aSelector ...
- itextsharp报错PdfReader not opened with owner password
itextSharp读取Pdf时报错:PdfReader not opened with owner password 报错原因:pdf文件被用户加密了. 解决办法:在创建pdfReader实例后,加 ...
- SP四种作用范围pageContext、request、session、application 一看就懂
作用域规定的是变量的有效期限: 1.如果把变量放到pageContext里,就说明它的作用域是page,它的有效范围只在当前jsp页面里. 从把变量放到pageContext开始,到jsp页面结束 ...
- 《DSP using MATLAB》Problem 7.12
阻带衰减50dB,我们选Hamming窗 代码: %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ...