当前位置:网站首页>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;
}
}
边栏推荐
- Mmseg - Mutli view time series data inspection and visualization
- Laravel generate entity
- Zhubo Huangyu: it's really bad not to understand these gold frying skills
- Redis6 transaction and locking mechanism
- ELK 企业级日志分析系统
- Require, require in PHP_ once、include、include_ Detailed explanation of the efficiency of repeated introduction of once class library
- What is information security? What is included? What is the difference with network security?
- Apicloud studio3 WiFi real machine synchronization and WiFi real machine preview instructions
- 面试官灵魂拷问:为什么代码规范要求 SQL 语句不要过多的 join?
- PHP character capture notes 2020-09-14
猜你喜欢
The development of speech recognition app with uni app is simple and fast.
The real king of caching, Google guava is just a brother
锚点导航小demo
Nantong online communication group
内网穿透工具 netapp
NFT value and white paper acquisition
Convolutional Neural Networks简述
Usage, installation and use of TortoiseSVN
牛客网:拦截导弹
ZABBIX monitoring
随机推荐
蓝桥杯学习2022.7.5(上午)
js 从一个数组对象中取key 和value组成一个新的对象
Usage, installation and use of TortoiseSVN
4年工作经验,多线程间的5种通信方式都说不出来,你敢信?
Aspx simple user login
我为什么支持 BAT 拆掉「AI 研究院」
redis6数据类型及操作总结
研究生可以不用学英语?只要考研英语或六级分数高!
Attack and defense world crypto WP
How to divide a large 'tar' archive file into multiple files of a specific size
Jasypt configuration file encryption | quick start | actual combat
Liar report query collection network PHP source code
Introduction to Chapter 8 proof problem of njupt "Xin'an numeral base"
Ordering system based on wechat applet
基于微信小程序的订餐系统
Wechat app payment callback processing method PHP logging method, notes. 2020/5/26
Kafaka log collection
Wonderful express | Tencent cloud database June issue
leetcode 10. Regular Expression Matching 正则表达式匹配 (困难)
These 18 websites can make your page background cool