当前位置:网站首页>The third question of force deduction -- the longest substring without repeated characters
The third question of force deduction -- the longest substring without repeated characters
2022-07-25 05:00:00 【InfoQ】
Preface

Topic analysis

The solution as fierce as a tiger
public static int LengthOfLongestSubstring2(string? s)
{
if(string.IsNullOrEmpty(s))
{
return 0;
}
if(s.Length==1)
{
return 1;
}
char[] chars = s.ToCharArray();
Hashtable hashtable = new Hashtable();
int index = 0;
int maxLen = chars.Length;
List<int> list = new List<int>();
while (index < maxLen)
{
hashtable.Clear();
for (int i = index; i < chars.Length; i++)
{
if (hashtable.ContainsKey(chars[i]))
{
list.Add(hashtable.Count);
index++;
break;
}
hashtable.Add(chars[i], i);
if (i == chars.Length - 1)
{
index++;
list.Add(hashtable.Count);
}
}
}
list.Sort((x, y) => -x.CompareTo(y));
return list[0];
}- First rule out special circumstances , Empty input and single character
- General characters , Natural division
- Within the specified cycle ( In fact, it is also to shorten the number of cycles step by step ), utilize hashtable Characteristics of , Store each byte and index value in the hash table
- Each cycle counts the value of the hash table into List in
- At the end of the cycle , Arrangement List, Sort in reverse order of size , The first value is the value required by the topic .
Standard solution
public static int LengthOfLongestSubstring(string? s)
{
// Hash set , Record whether each character appears
HashSet<Char> occ = new HashSet<Char>();
int n = string.IsNullOrEmpty(s) ? 0 : s.Length;
// Right pointer , The initial value is -1, It's like we're on the left side of the left bound of the string , It's not moving yet
int rk = -1, ans = 0;
for (int i = 0; i < n; ++i)
{
if (i != 0)
{
// The left pointer moves one space to the right , Remove a character
occ.Remove(s[i - 1]);
}
while (rk + 1 < n && !occ.Contains(s[rk + 1]))
{
// Keep moving the right pointer
occ.Add(s[rk + 1]);
++rk;
}
// The first i To rk A character is a very long non repeating character substring
ans = Math.Max(ans, rk - i + 1);
}
return ans;
}
边栏推荐
- 85 distributed project construction
- Most of the time, it's probability
- Small case of data analysis: visualize recruitment data and view the most needed technologies in the field~
- [sht30 temperature and humidity display based on STM32F103]
- Unity LOD
- Tiny-emitter.js: a small event subscription and Publishing Library
- How can test / development programmers with 5 years of experience break through the technical bottleneck? Common problems in big factories
- Gbase 8A about no suitable driver
- 【微信小程序】拍卖商品详情页设计与交互实现(包含倒计时、实时更新出价)
- Style transfer -- CCPL: contrast coherence preserving loss for versatile style transfer
猜你喜欢

Salt and ice particles cannot be distinguished

I will write some Q & A blogs recently, mainly focusing on the points that are easy to have doubts.

Your technical leader doesn't understand this? Without it, there is no complete thinking process of design

How to publish your own NPM package

Ora-01460: conversion request cannot be implemented or unreasonable

Completed project series Tutorials - smart campus management system
![[wechat applet] design and interactive implementation of auction product details page (including countdown and real-time update of bids)](/img/01/42de6280191b9c32a7f37d7727bd4f.png)
[wechat applet] design and interactive implementation of auction product details page (including countdown and real-time update of bids)

Interviewer: explain the core principle of ThreadLocal

Getting started with scratch

Teach you three ways to optimize the performance from 20s to 500ms
随机推荐
When image component in wechat applet is used as background picture
基于云原生的私有化 PaaS 平台交付实践
[small program practice] first day
5年经验的大厂测试/开发程序员,怎样突破技术瓶颈?大厂通病......
小红书携手HMS Core,畅玩高清视界,种草美好生活
【浅析STM32之GPIO寄存器(CRL/CRH)配置 】
今天很重要
Openworm project compilation
Deep understanding of pod
Logu p3398 hamsters find sugar solution
很多时候都是概率
Your technical leader doesn't understand this? Without it, there is no complete thinking process of design
Most of the time, it's probability
搭建私有CA服务器
QT download installation tutorial
Xiaohongshu joins hands with HMS core to enjoy HD vision and grow grass for a better life
STM32 development note 120: solve the problem that%f in printf cannot be output
2022-7-13 summary
Very clear organization
一篇文章带你读懂Redis的哨兵模式