当前位置:网站首页>Leetcode selected top interview questions_ character string
Leetcode selected top interview questions_ character string
2022-07-20 14:05:00 【Figure throne】
3. Longest substring without repeating characters
What is a sliding window ?
It's actually a queue , For example, in the example abcabcbb, Enter this line ( window ) by abc Meet the requirements of the topic , When you enter a, The queue becomes abca, Not meeting the requirements at this time . therefore , We're going to move this queue !
How to move ?
All we have to do is move the elements on the left side of the queue , Until the subject requirements are met ! Keep this line up all the time , Find out the maximum length of the queue , Find out the solution !
Time complexity :O(n).
Java resolvent
A data structure is needed to determine whether there are repeated characters :HashSet.
class Solution {
public int lengthOfLongestSubstring(String s) {
Set<Character> ss = new HashSet<Character>();
int slen = s.length();
int length = 0;
int maxLength = 0;
for(int i = 0; i < slen; i++)
{
// Exit an element in the queue
if(i != 0)
{
ss.remove(s.charAt(i - 1));
}
// Add elements to the queue , join HashSet Will automatically determine whether there is repetition
while(length < slen && ss.add(s.charAt(length)))
{
length++;
}
if(maxLength < length - i)
{
maxLength = length - i;
}
}
return maxLength;
}
}
C Language solutions
bool haveSame(char *ss, int start, int end, char c)
{
int i = start;
if(end == 0)
{
return true;
}
while(i < end && ss[i] != c)
{
i++;
}
if(i >= end)
{
return true;
}
else
{
return false;
}
}
int lengthOfLongestSubstring(char * s){
char *ss = NULL;
int len;
len = strlen(s);
ss = (char *)malloc(len * sizeof(char));
int length = 0;
int maxLength = 0;
for(int i = 0; i < len; i++)
{
// Add elements to the queue , join HashSet Will automatically determine whether there is repetition
while(length < len && haveSame(ss, i, length, s[length]))
{
ss[length] = s[length];
length++;
}
if(maxLength < length - i)
{
maxLength = length - i;
}
}
return maxLength;
}
边栏推荐
- What is the difference between SSL and TLS
- 恒勃控股IPO过会:年营收6.3亿 周书忠家族为实控人
- 4.5 nodejs自动化测试设计思路2
- 新莱福新材IPO过会:年营收7.8亿 汪小明持有47%股权
- [latex] ppt Drawing, Export EMF format, word insert EMF file and export PDF, PDF cut and export EPS file, latex insert EPS file
- 9、gorm进阶
- 云原生(三十六) | Kubernetes篇之Harbor入门和安装
- Use custom rrt* global planner to create map navigation
- web:从10到1的编译大重构
- 2021 年6月面试遭遇滑铁卢,现在这么内卷了吗
猜你喜欢
技术管理进阶——团队一盘散沙,怎么破?
如何用 UDP 实现可靠传输?
【Latex】PPT畫圖,導出emf格式,word插入emf文件並導出pdf,pdf裁剪並導出eps文件,latex插入eps文件
[latex] ppt Drawing, Export EMF format, word insert EMF file and export PDF, PDF cut and export EPS file, latex insert EPS file
国际顶会OSDI首度收录淘宝系统论文,端云协同智能获大会主旨演讲推荐
【Redux】我用一张图彻底理解了Redux的工作流程(附案例、源码)
如何使用缓冲队列实现高并发下单业务(荣耀典藏版)
[latex] ppt drawing, exporting EMF format, word inserting EMF file and exporting PDF, PDF clipping and exporting EPS file, latex inserting EPS file
8、ORM简介与gorm入门
如何使用 SAP Intelligent Robotic Process Automation 自动操作 Excel
随机推荐
Trial record of ModelBox end cloud collaborative AI development kit (rk3568) (I)
Xinlaifu Xincai IPO meeting: annual revenue of 780million, with 47% equity held by Wang Xiaoming
系统App 签名JKS制作及静默安装
What is the difference between SSL and TLS
人均瑞数系列,瑞数 4 代 JS 逆向分析
Use of ADB debugging tools
如何使用缓冲队列实现高并发下单业务(荣耀典藏版)
SSH 私钥实现登录远程目标服务器
LiveDataBus核心原来如此简单
AutoJs学习-实现3D视角
今日睡眠质量记录75分
熔断、降级 Sentinel
openpcdet之pointpillar代码阅读——第三篇:损失函数的计算
基于pexels 图片素材api,整理出素材资源库
AutoJs学习-文件深度搜索
SSL与TLS到底有何区别,一见分晓
Detailed explanation of Android interview hash principle II
网关Gateway的介绍(下)
List of common plug-ins for idea Community Edition
Mysql引擎介绍及InnoDB逻辑存储结构