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 思 ...
随机推荐
- python函数进阶(函数参数、返回值、递归函数)
函数进阶 目标 函数参数和返回值的作用 函数的返回值 进阶 函数的参数 进阶 递归函数 01. 函数参数和返回值的作用 函数根据 有没有参数 以及 有没有返回值,可以 相互组合,一共有 4 种 组合形 ...
- python实现算术表达式的词法语法语义分析(编译原理应用)
本学期编译原理的一个大作业,我的选题是算术表达式的词法语法语义分析,当时由于学得比较渣,只用了递归下降的方法进行了分析. 首先,用户输入算术表达式,其中算术表达式可以包含基本运算符,括号,数字,以及用 ...
- 11--Python入门--面向对象
面向对象是Python的特点.面向对象主要通过类class的定义来实现.类class是用来描述具有相同属性和方法的对象的集合.类定义了该集合中的每个对象的共有属性和方法可以将类理解为一个模块,模块中包 ...
- 结构体的数据对齐 #pragma浅谈
之前若是有人拿个结构体或者联合体问我这个结构占用了多少字节的内存,我一定觉得这个人有点low, 直到某某公司的一个实习招聘模拟题的出现,让我不得不重新审视这个问题, 该问题大致如下: typedef ...
- Gym - 101617F :Move Away (圆的交点)
pro:给定N个圆,求离原点最远的点,满足它在N个圆里.输出这个距离.N<50; sol:关键点一定是圆与圆的交点. 圆与 圆心到原点的直线 的交点. 然后去验证这些关键点是否在N个圆内. 实际 ...
- ES5与ES6中的继承
ES5继承在ES5中没有类的概念,所以一般都是基于原型链继承,具体的继承方法有以下几种: 父类: function Father (name) { this.name = name || 'sam' ...
- windows下使用kafka的常用命令
参考文档: https://blog.csdn.net/evankaka/article/details/52421314 http://orchome.com/6 1 启动zookeeper cmd ...
- node day2 vue read html
app.js var http = require("http"); var fs = require('fs'); var url = require('url'); http. ...
- C++中字符数组与string的相互转换
字符数组转化成string类型char ch [] = "ABCDEFG";string str(ch);//也可string str = ch;或者char ch [] = &q ...
- 互联网媒体类型 MIME Type
参考:https://zh.wikipedia.org/wiki/%E4%BA%92%E8%81%94%E7%BD%91%E5%AA%92%E4%BD%93%E7%B1%BB%E5%9E%8B 互联网 ...