当前位置:网站首页>力扣题解8/3
力扣题解8/3
2022-08-04 05:05:00 【缘聚654】
难度中等7944
给定一个字符串 s
,请你找出其中不含有重复字符的 最长子串 的长度。
示例 1:
输入: s = "abcabcbb"
输出: 3
解释: 因为无重复字符的最长子串是 "abc",所以其
长度为 3。
示例 2:
输入: s = "bbbbb"
输出: 1
解释: 因为无重复字符的最长子串是 "b"
,所以其长度为 1。
示例 3:
输入: s = "pwwkew" 输出: 3 解释: 因为无重复字符的最长子串是"wke"
,所以其长度为 3。 请注意,你的答案必须是 子串 的长度,"pwke"
是一个子序列,不是子串 。
本题采用滑动窗口法,即设两个变量right和left来指向一段字符的两端作为一个窗口,那么字符的长度就表示为
right-left+1;
那么如何找出最长无重复字符长呢
首先使用一个外部循环用来结束查找,然后再用一个内部循环来判断字符串从left到right是否有重复字符,如果有那么就讲使left前进,将重复字符去除,同时还要进行对比以此来保留最大无重复字符串的值,直到right进行到末尾。
完整代码如下
int lengthOfLongestSubstring(char * s){
int left=0;
int right=0;
int n;
n=strlen(s);
int i,j,max=0;
int havesameChar;
for(i=0;i<n;i++)
{
if(left<=right)
{
havesameChar=0;
for(j=left;j<right;j++)
{
if(s[j]==s[right])
{
havesameChar=1;
break;
}
}
if(havesameChar)
{
left=j+1;
}
}
max=max<(right-left+1)?(right-left+1):max;
right++;
}
return max;
}
边栏推荐
- [21 Days Learning Challenge] Image rotation problem (two-dimensional array)
- mysql索引笔记
- Will the 2023 PMP exam use the new version of the textbook?Reply is here!
- 7-2 LVS+DR Overview and Deployment
- 烧录场景下开发如何进行源代码保密工作
- Get the selected content of the radio box
- 7-1 LVS+NAT load balancing cluster, NAT mode deployment
- Dynamic programming of the division of numbers
- Interesting Kotlin 0x0E: DeepRecursiveFunction
- There is an 8 hour difference between the docker installation of mysql and the host.
猜你喜欢
[Cloud Native--Kubernetes] Pod Resource Management and Probe Detection
Structure function exercise
7-2 LVS+DR概述与部署
信息学奥赛一本通 1312:【例3.4】昆虫繁殖
[C language advanced] program environment and preprocessing
el-Select selector bottom fixed
看DevExpress丰富图表样式,如何为基金公司业务创新赋能
About yolo7 and gpu
震惊,99.9% 的同学没有真正理解字符串的不可变性
【一步到位】Jenkins的安装、部署、启动(完整教程)
随机推荐
10 Convolutional Neural Networks for Deep Learning 3
C专家编程 第4章 令人震惊的事实:数组和指针并不相同 4.4 使声明与定义相匹配
商城App开发都有哪些功能呢
How to keep the source code confidential in the development under the burning scenario
C Expert Programming Chapter 4 The Shocking Fact: Arrays and pointers are not the same 4.4 Matching declarations to definitions
7-3 LVS+Keepalived Cluster Description and Deployment
结构体函数练习
el-Select selector bottom fixed
备份工具pg_dump的使用《postgres》
Basic characteristics of TL431 and oscillator circuit
JVM Notes
【一步到位】Jenkins的安装、部署、启动(完整教程)
商城系统APP如何开发 都有哪些步骤
2022 software test interview questions The latest ByteDance 50 real interview questions, 15k have been won after brushing, with explanation + Q&A
[Cocos] cc.sys.browserType可能的属性
idea设置识别.sql文件类型以及其他文件类型
Towards Real-Time Multi-Object Tracking(JDE)
2022年PMP考试延迟了,该喜该忧?
你以为border-radius只是圆角吗?【各种角度】
Large chain best freight d audit with what software?What are the functions?