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的更多相关文章

  1. Leetcode3:Longest Substring Without Repeating Characters@Python

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

  2. LeetCode3:Longest Substring Without Repeating Characters

    题目: Given a string, find the length of the longest substring without repeating characters. For examp ...

  3. 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 ...

  4. leetcode3:不重复的最长子串长度

    package hashmap; import java.util.HashMap; import java.util.Map; public class hashmap { public stati ...

  5. LeetCode3 Longest Substring Without Repeating Characters

    题意: Given a string, find the length of the longest substring without repeating characters. Examples: ...

  6. leetcode3 Two Sum III – Data structure design

    Question: Design and implement a TwoSum class. It should support the following operations: add and f ...

  7. [Swift]LeetCode3. 无重复字符的最长子串 | Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

  8. leetcode3:无重复字符的最长子串

    给定一个字符串,找出不含有重复字符的最长子串的长度. 示例: 给定 "abcabcbb" ,没有重复字符的最长子串是 "abc" ,那么长度就是3. 给定 &q ...

  9. 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 思 ...

随机推荐

  1. [SQL]Temporal 异常处理经验

    解决20762 Module 3 课程关于Temporal Table 在 Demo 时的错误 首先该Demo 主要是搭配AdvantureWorks2016 的资料库,使用Person.Person ...

  2. 开发方式-----C语言

    上期我们已经把C语言的开发平台搭建好了,还有不清楚地可以查看我上一篇的笔记,这次我们就要进行编辑C语言,那么它到底是怎么实现开发的呢?这一期我就来演示一次开发方式,至于说明为什么会这样或者这个是什么意 ...

  3. case语法

    一.文件系统访问列表 FACL :Filesystem Access Control List    文件系统访问列表 利用文件扩展保存额外的访问控制权限. setfacl: -m:设定访问控制权限  ...

  4. JS的异步操作

    异步操作: 1.定时器都是异步操作 2.事件绑定都是异步操作 3.AJAX中一般我们都采用异步操作 4.回调函数可以理解为异步 同步:一次只能完成一个任务,如果多个任务就必须排队,先前面一个任务再执行 ...

  5. Java中==和equals的比较

    1.== (1)基本数据类型:比较值  基本类型有8种: 整数型:byte 字节类型:1个字节(8位) short 短整型:2个字节(16位) int  整数类型:4个字节(32位)   Java默认 ...

  6. Python 进程池的同步方法

    import time from multiprocessing import Process,Pool def f1(n): time.sleep(1) #print(n) return n*n i ...

  7. 剑指Offer 33. 丑数 (其他)

    题目描述 把只包含质因子2.3和5的数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因为它包含质因子7. 习惯上我们把1当做是第一个丑数.求按从小到大的顺序的第N个丑数. 题目地 ...

  8. 自动化测试-19.selenium定位之通过JS修改html写入日期数据以及从文本读取数据实战

    # -*- coding: utf-8 -*- from selenium import webdriver from selenium.webdriver.support.select import ...

  9. 获取China大陆IP段的范围

    这里有几个网站提供了大陆的IP段范围.别问我要这个列表干什么,我也不知道. http://www.ip2location.com/blockvisitorsbycountry.aspx老牌网站,国内很 ...

  10. ubuntu16.04下 sublime text输入中文

    1.git clone https://github.com/lyfeyaj/sublime-text-imfix.git 2.cd sublime-text-imfix && ./s ...