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 思 ...
随机推荐
- Java语法基础学习DaySixteen(多线程)
一.多线程的创建 1.作用 程序需要同时执行两个或多个任务时需要多线程. 程序需要实现需要等待的任务时,如用户输入.文件读写操作.网络操作.搜索等,需要多线程. 需要一些后台运行的程序时,需要多线程. ...
- error ...项目路径 \node_modules\chromedriver: Command failed.
当你拿到完整的vue项目代码,像 node_modules 整个文件夹不可能提交到svn或者github的, 所以怎么自己安装整个项目所需要的一些依赖呢, 前提是你全局安装了node,才能使用nom命 ...
- 如何在QFileSystemModel中显示文件夹的大小
在Qt里面,有一种Model/View框架,Model负责收集信息,View负责显示信息.QFileSystemModel可以读取文件大小,但是默认情况下不能读取文件夹大小. QFileSystemM ...
- sed语法2
sed命令是一个面向字符流的非交互式编辑器,也就是说sed不允许用户与它进行交互操作.sed是按行来处理文本内容的.在shell中,使用sed来批量修改文本内容是非常方便的. sed命令的选项 sed ...
- idea设置代码提示不区分大小写
idea设置代码提示不区分大小写 intellij idea默认下的代码提示是区分大小写的,例如类方法名过长.类的名字过长等,完全通过手打的话较为繁琐,这里简单的设置下即可. 把 Case sensi ...
- leetcode题解 3. Longest Substring Without Repeating Characters
题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...
- DG_数据文件转换参数测试
本篇博客流程图: 一.测试需求及参数说明 二.测试环境进行相关测试 三.问题总结 一.测试需求及参数说明 1.1测试需求说明 DG切换 切换前,数据库版本12.2.0.1,主库rac两节点,备一rac ...
- Spring 自动装配及其注解
一.属性自动装配 首先,准备三个类,分别是User,Cat,Dog.其中User属性拥有Cat和Dog对象. package com.hdu.autowire; public class User { ...
- 我的代码-models
# coding: utf-8 # In[1]: import pandas as pdimport numpy as npfrom sklearn import treefrom sklearn.s ...
- torchvision里densenet代码分析
#densenet原文地址 https://arxiv.org/abs/1608.06993 #densenet介绍 https://blog.csdn.net/zchang81/article/de ...