当前位置:网站首页>longest substring without repeating characters
longest substring without repeating characters
2022-08-09 06:33:00 【In the history of the strongest disciple】
给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度.
输入: 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));
}
}
执行结果:
边栏推荐
- Program Performance Analysis - Complexity Analysis
- 05 多线程与高并发 - ThreadPoolExecutor 源码解析
- BeautifulSoup4的介绍与使用
- 【R语言】把文件夹下的所有文件提取到特定文件夹
- 什么是excel文件保护
- Simple Factory Pattern
- Invalid argument(s) appears when redis runs lua script
- 如何 认识与学习BASH
- 事务总结
- Remember a nest.js route that matches all the path problems that follow
猜你喜欢
Inception V3 闭眼检测
C语言实现顺序栈和链队列
CMake中INSTALL_RPATH与BUILD_RPATH问题
Error jinja2.exceptions.UndefinedError: 'form' is undefined
运放-运算放大器经典应用电路大全-应用电路大全
C# 利用iTextSharp画PDF
【Feel】In the Unity Feel plugin, Camera cannot display CameraShake correctly
字节跳动笔试题2020 (抖音电商)
Invalid argument(s) appears when redis runs lua script
pdf加密、找回密码
随机推荐
Use of PlantUML plugin in idea
【Wwise】ArgumentException: The specified path is not of a legal form (empty). About the path reading error in WwiseGlobal
治疗消化性溃疡—Toronto Research Chemicals 甘氨酸铝
[HNOI2002]营业额统计
APP product source data interface (taobao, jingdong/spelling/suning/trill platform details a lot data analysis interface) code and docking tutorial
IQ Products巨细胞病毒CMV感染检测试剂盒的特征和应用
io.lettuce.core。RedisCommandTimeoutException命令超时
golang zip aes base64
常用Oracle命令
pdf加密、找回密码
CMake中INSTALL_RPATH与BUILD_RPATH问题
中英文说明书丨CalBioreagents 醛固酮单克隆抗体
Program Performance Analysis - Complexity Analysis
leetcode 之 零移位
[MySQL] Second, the relationship between processes, MySQL password cracking, table building and database building related commands
直接用的zip包 缺少很多依赖,pip没有,感觉用anaconda create一个环境会方便点
安装flask
程序性能分析 —— 复杂度分析
golang xml 处理动态属性
一道很简答但是没答对的SQL题