当前位置:网站首页>LeetCode_3(无重复字符的最长子串)
LeetCode_3(无重复字符的最长子串)
2022-07-05 13:51:00 【***】
题目描述:
给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。
示例 1:
输入: s = “abcabcbb”
输出: 3
解释: 因为无重复字符的最长子串是 “abc”,所以其长度为 3。
示例 2:
输入: s = “bbbbb”
输出: 1
解释: 因为无重复字符的最长子串是 “b”,所以其长度为 1。
示例 3:
输入: s = “pwwkew”
输出: 3
解释: 因为无重复字符的最长子串是 “wke”,所以其长度为 3。请注意,你 的答案必须是 子串 的长度,“pwke” 是一个子序列,不是子串。
提示:
0 <= s.length <= 5 * 104
s 由英文字母、数字、符号和空格组成
class Solution {
public int lengthOfLongestSubstring(String s) {
int ans=0;
if(s.equals(""))return 0;
if(s.length()==1)return 1;
int i=0,j=1,len=1;
while(j<s.length()){
//遍历
for (int k = i; k < j; k++) {
//窗口内遍历
if(s.charAt(k)==s.charAt(j)){
//判断窗口内是否有重复字符
i=k+1;
len=j-i;
break;
}
}
len++;
if(len>ans)ans=len;
j++;
}
return ans;
}
}
边栏推荐
- Introduction to Chapter 8 proof problem of njupt "Xin'an numeral base"
- MySQL if else use case use
- Idea remote debugging agent
- Liste des liens (simple)
- 链表(简单)
- Laravel框架运行报错:No application encryption key has been specified
- Kotlin协程利用CoroutineContext实现网络请求失败后重试逻辑
- Rk3566 add LED
- 搭建一个仪式感点满的网站,并内网穿透发布到公网 2/2
- ETCD数据库源码分析——集群间网络层客户端peerRt
猜你喜欢
Catch all asynchronous artifact completable future
[machine learning notes] several methods of splitting data into training sets and test sets
【云资源】云资源安全管理用什么软件好?为什么?
Usage, installation and use of TortoiseSVN
几款分布式数据库的对比
Redis6 transaction and locking mechanism
Jasypt configuration file encryption | quick start | actual combat
深拷贝真难
Wonderful express | Tencent cloud database June issue
TortoiseSVN使用情形、安装与使用
随机推荐
Aikesheng sqle audit tool successfully completed the evaluation of "SQL quality management platform grading ability" of the Academy of communications and communications
redis6事务和锁机制
Linked list (simple)
[public class preview]: basis and practice of video quality evaluation
Usage, installation and use of TortoiseSVN
What are the private addresses
Resttemplate details
2022建筑焊工(建筑特殊工种)特种作业证考试题库及在线模拟考试
Log4j utilization correlation
蓝桥杯学习2022.7.5(上午)
Personal component - message prompt
Wonderful express | Tencent cloud database June issue
Network security HSRP protocol
这18个网站能让你的页面背景炫酷起来
NFT value and white paper acquisition
ETCD数据库源码分析——rawnode简单封装
2022司钻(钻井)考试题库及模拟考试
几款分布式数据库的对比
【华南理工大学】考研初试复试资料分享
研究生可以不用学英语?只要考研英语或六级分数高!