当前位置:网站首页>无重复的字符的最长子串
无重复的字符的最长子串
2022-08-09 06:29:00 【史上最强的弟子】
给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。
输入: s = "abcabcbb"
输出: 3
解释: 因为无重复字符的最长子串是 "abc",所以其
长度为 3。
输入: s = "bbbbb"
输出: 1
解释: 因为无重复字符的最长子串是 "b"
,所以其长度为 1。
示例 3:
输入: s = "pwwkew"
输出: 3
解释: 因为无重复字符的最长子串是 "wke"
,所以其长度为 3。
示例 4:
输入: s = "" 输出: 0
实现代码: (双指针)
public class Test23 {
public int getMaxSubStr(String subStr){
if(subStr.equals("")){
return 0;
}
HashSet<Character> hashSet = new HashSet<>();
int returnObject = 0;
for(int i=0;i<subStr.length();i++){
hashSet.add(subStr.charAt(i));
for(int j=i+1;j<subStr.length();j++){
//如果hashSet 中有字符,
if(hashSet.contains(subStr.charAt(j))){
break;
}
hashSet.add(subStr.charAt(j));
}
returnObject = Math.max(hashSet.size(),returnObject);
hashSet.removeAll(hashSet);
}
return returnObject;
}
public static void main(String[] args) {
String s = "pwwkew";
System.out.println("s:"+s+",return:"+new Test23().getMaxSubStr(s));
}
}
实现代码(滑动窗口)
/*
给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。
示例 1:
输入: s = "abcabcbb"
输出: 3
解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。
示例 2:
输入: s = "bbbbb"
输出: 1
解释: 因为无重复字符的最长子串是 "b",所以其长度为 1。
示例 3:
输入: s = "pwwkew"
输出: 3
解释: 因为无重复字符的最长子串是 "wke",所以其长度为 3。
请注意,你的答案必须是 子串 的长度,"pwke" 是一个子序列,不是子串。
示例 4:
输入: s = ""
输出: 0
提示:
0 <= s.length <= 5 * 104
s 由英文字母、数字、符号和空格组成
*/
import java.util.HashSet;
import java.util.Set;
public class Test23 {
/*public int getMaxSubStr(String subStr){
if(subStr.equals("")){
return 0;
}
HashSet<Character> hashSet = new HashSet<>();
int returnObject = 0;
for(int i=0;i<subStr.length();i++){
hashSet.add(subStr.charAt(i));
for(int j=i+1;j<subStr.length();j++){
//如果hashSet 中有字符,
if(hashSet.contains(subStr.charAt(j))){
break;
}
hashSet.add(subStr.charAt(j));
}
returnObject = Math.max(hashSet.size(),returnObject);
hashSet = new HashSet<>();
}
return returnObject;
}*/
public int getMaxSubStr2(String subStr){
HashSet<Character> hashSet = new HashSet<>();
int returnObject = 0;
int rk =0 ;
for(int i=0;i<subStr.length();i++){
if(i!=0){
hashSet.remove(subStr.charAt(i-1));
}
for(int j=rk;j<subStr.length();j++){
//如果hashSet 中有字符,
if(hashSet.contains(subStr.charAt(j))){
break;
}
hashSet.add(subStr.charAt(j));
rk++;
}
returnObject = Math.max(hashSet.size(),returnObject);
}
return returnObject;
}
public int lengthOfLongestSubstring(String s) {
// 哈希集合,记录每个字符是否出现过
Set<Character> occ = new HashSet<Character>();
int n = s.length();
// 右指针,初始值为 -1,相当于我们在字符串的左边界的左侧,还没有开始移动
int rk = -1, ans = 0;
for (int i = 0; i < n; ++i) {
if (i != 0) {
// 左指针向右移动一格,移除一个字符
occ.remove(s.charAt(i - 1));
}
while (rk + 1 < n && !occ.contains(s.charAt(rk + 1))) {
// 不断地移动右指针
occ.add(s.charAt(rk + 1));
++rk;
}
// 第 i 到 rk 个字符是一个极长的无重复字符子串
ans = Math.max(ans, rk - i + 1);
}
return ans;
}
public static void main(String[] args) {
//String s = "dvdf";
String s = "acdddskwleod";
Test23 test23 = new Test23();
System.out.println("s:"+s+",return:"+test23.getMaxSubStr2(s));
System.out.println(test23.lengthOfLongestSubstring(s));
}
}
执行结果:
边栏推荐
- 线程的6种状态
- 运放-运算放大器经典应用电路大全-应用电路大全
- Unity backgammon game design and simple AI implementation (1)
- Search 1688 product interface by image (item_search_img-search 1688 product by image (Politao interface) code docking tutorial
- 输入框最前面添加放大镜&&background-image和background-color冲突问题
- 移远EC20 4G模块拨号相关
- jdepend
- 推进产教融合 赋能教育创新发展 | 华云数据荣获“企业贡献奖”
- [GO], arrays and slices
- Likou Brush Question 180
猜你喜欢
Output method of list string print(*a) print(““.join(str(c) for c in a) )
Use of PlantUML plugin in idea
运算放大器(OPA)超详细参数讲解-运放---以及8个型号的运算放大器分析对比
2022-08-08: Given an array arr, it represents the height of the missiles that will appear in order from morning to night.When the cannon shoots missiles, once the cannon is set to shoot at a certain h
phpstudy install flarum forum
zip压缩包密码解密
The solution that does not work and does not take effect after VScode installs ESlint
Deep Learning - Principles of Neural Networks 2
CMake中INSTALL_RPATH与BUILD_RPATH问题
Adds, deletes, searches, and changes the leading doubly circular linked list (implemented in C language)
随机推荐
crc calculation
Unity 五子棋游戏设计和简单AI(2)
【Wwise】ArgumentException: The specified path is not of a legal form (empty).关于WwiseGlobal中的路径读取错误问题
pdf加密、找回密码
Flask failed to create database without error
详解C语言中的wait()函数和waitpid()函数
普罗米修斯原理及节点发布
Silently start over, the first page is also a new page
sql问题解答创建表的语句
抗菌药物丨Toronto Research Chemicals 天冬酰胺D
Gao Zelong, a famous digital collection expert and founder of the Digital Collection Conference, was interviewed by China Entrepreneur Magazine
Introduction to AIOT
How to find package information and pin definitions for NXP S32K1xx series microcontrollers
C语言的内置宏(定义日志宏)
APP商品详情源数据接口(淘宝/京东/拼多多/苏宁/抖音等平台详情数据分析接口)代码对接教程
什么是excel文件保护
Qt learning (3) - Qt module
[GO], arrays and slices
报错:flask: TypeError: ‘function‘ object is not iterable
使用百度EasyDL实现智能垃圾箱