当前位置:网站首页>力扣题解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;
}边栏推荐
- 7. The principle description of LVS load balancing cluster
- Use Patroni callback script to bind VIP pit
- About yolo7 and gpu
- What are the functions of mall App development?
- The difference between px, em, and rem
- el-Select selector bottom fixed
- C Expert Programming Chapter 5 Thinking about Linking 5.3 5 Special Secrets of Library Linking
- C专家编程 第5章 对链接的思考 5.3 函数库链接的5个特殊秘密
- 深度学习21天——卷积神经网络(CNN):实现mnist手写数字识别(第1天)
- 数的划分之动态规划
猜你喜欢

What is the salary of a software testing student?

7-1 LVS+NAT load balancing cluster, NAT mode deployment

There is an 8 hour difference between the docker installation of mysql and the host.

【一步到位】Jenkins的安装、部署、启动(完整教程)

10 Convolutional Neural Networks for Deep Learning 3

烧录场景下开发如何进行源代码保密工作

Resolved error: npm WARN config global `--global`, `--local` are deprecated

附加:对于“与数据表对应的实体类“,【面对MongoDB时,使用的@Id等注解】和【以前面对MySQL时,使用的@Id等注解】,是不同的;

详解八大排序

转:管理是对可能性的热爱,管理者要有闯进未知的勇气
随机推荐
震惊,99.9% 的同学没有真正理解字符串的不可变性
drools从下载到postman请求成功
[Skill] Using Sentinel to achieve priority processing of requests
C Expert Programming Chapter 4 The Shocking Fact: Arrays and Pointers Are Not the Same 4.3 What is a Declaration and What is a Definition
Write golang simple C2 remote control based on gRPC
leetcode 12. Integer to Roman numeral
There is an 8 hour difference between the docker installation of mysql and the host.
C专家编程 第4章 令人震惊的事实:数组和指针并不相同 4.2 我的代码为什么无法运行
【21天学习挑战赛】图像的旋转问题(二维数组)
day13--postman接口测试
How to view sql execution plan offline collection
QT 如何识别文件的编码格式
2022年PMP考试延迟了,该喜该忧?
7-3 LVS+Keepalived Cluster Description and Deployment
C专家编程 第5章 对链接的思考 5.2 动态链接的优点
Get the selected content of the radio box
中信证券网上开户怎么开的?安全吗?
【21 Days Learning Challenge】Direct Insertion Sort
C专家编程 第5章 对链接的思考 5.6 轻松一下---看看谁在说话:挑战Turning测验
有趣的 Kotlin 0x0E:DeepRecursiveFunction