3. Longest Substring Without Repeating Characters寻找不重复的最大子串
首先弄清楚Substring和Subsequence,前者是子串,要求连续,后者是子序列,可以不连续
public int lengthOfLongestSubstring(String s) {
/*
一开始自己想的方法:两个指针,一个作为开始,一个用来遍历,复杂度O(N)
*/
int l = s.length();
if (l==0)
return 0;
char[] str = s.toCharArray();
int res = 1;
Set<Character> set = new HashSet<>();
for (int i = 0; i < l-1; i++) {
set.add(str[i]);
for (int j = i+1; j < l; j++) {
if (set.contains(str[j]))
break;
set.add(str[j]);
res = Math.max(res,set.size());
}
set.clear();
}
return res;
}
public int lengthOfLongestSubstring2(String s) {
/*
O(N)做法,自己建立哈希表来记录有没有出现过,第一次接触这种做法,要记住
思路就是用哈希表记录当前字符最近一次出现时的index
如果没出现重复字符串就继续遍历,每次更新res长度
如果出现了重复,那就把最左边的坐标移到前边重复数字的下一个,然后继续遍历,res一直是记录最大长度
与这种做法相比,第一种做法每次都会遍历很多重复的子串
*/
//所有字符都能转化为256以内的int值,这样就可以记录所有字符
int[] index = new int[256];
Arrays.fill(index,-1);
//记录 最左边坐标
int left = 0;
//记录结果
int res = 0;
for (int i = 0; i < s.length(); i++) {
//先检测有没有重复字符,如果,left更新,比较的方法是把哈希表中的记录和left比较,取大的。如果没有出现过,那么哈希表中的记录是-1,肯定是left大
//如果出现过,有两种情况,一种是记录没有left大,说明是和left 之前的一个重复了,这种情况没有影响 ,另一种是比left大,这样就会影响长度的判断,要更新left
left = Math.max(left,index[s.charAt(i)+1]);
//更新哈希表
index[s.charAt(i)] = i;
res = Math.max(res,i-left+1);
}
return res;
}
3. Longest Substring Without Repeating Characters寻找不重复的最大子串的更多相关文章
- LeetCode——Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
- 【LeetCode】3 、Longest Substring Without Repeating Characters
题目等级:Medium 题目描述: Given a string, find the length of the longest substring without repeating chara ...
- LeetCode题解 || Longest Substring Without Repeating Characters (O(n)算法)问题
problem: Given a string, find the length of the longest substring without repeating characters. For ...
- LeetCode[3] Longest Substring Without Repeating Characters
题目描述 Given a string, find the length of the longest substring without repeating characters. For exam ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- 3. Longest Substring Without Repeating Characters(c++) 15ms
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- 【leetcode】Longest Substring Without Repeating Characters
题目描述: Given a string, find the length of the longest substring without repeating characters. For exa ...
- Longest Substring Without Repeating Characters(C语言实现)
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
随机推荐
- 利用python库stats进行t检验
t检验通常分为三种,分别是单样本t检验.双样本t检验和配对样本t检验.本文基于python的scipy.stats函数对每种t检验进行了介绍和实验. 一.t检验介绍 无论哪种t检验,都有以下的基本 ...
- Docker一些基本操作
1.停止所有的container,这样才能够删除其中的images: docker stop $(docker ps -a -q) 如果想要删除所有container的话再加一个指令: docker ...
- bulk_create 批量插入数据
def booklist(request): # 动态插入100条数据 for i in range(100): models.Book2.objects.create(name='第%s本书'%i) ...
- 用了Dapper之后就不要再见到SqlConnection咯
一.背景 前几天看公司一个新项目的底层使用了dapper,大家都知道dapper是一个非常强大的半自动化orm,帮程序员解决了繁琐的mapping问题,用起来非常爽,但我还是遇到了一件非常不爽的事情, ...
- (十八)面向流水线的设计:CPU的一心多用
一.单指令周期 由前可知,一条CPU指令的执行有三个步骤:指令读取.指令译码.指令执行.由于这个过程受CPU时钟的控制,如果我们将这个过程安排在一个CPU时钟周期内执行,这种设计思路就叫单 ...
- PyQt(Python+Qt)学习随笔:QListWidget的访问当前项的currentItem和setCurrentItem方法
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 currentItem方法返回列表部件当前选择的项,setCurrentItem方法用于设置当前项. ...
- Xpath基础学习
方法 获取文本 a/text() 获取a标签下的文本 a//text() 获取a标签下所有标签的文本 a[text()='xxx']获取文本为xxx的a标签 @符号 a/@href 获取a标签的hre ...
- 本地代码上传到github
一,注册Github账号 1.先注册一个账号,注册地址:https://github.com/ 2.登录后,点击start a project 3.创建一个repository name,输入框随便取 ...
- Scrum 冲刺第七天
一.每日站立式会议 1.会议内容 1)进行每日工作汇报 张博愉: 昨天已完成的工作:与林梓琦同学完成发帖模块的交接 今日工作计划:完善发帖模块的点赞.上传图片功能 工作中遇到的困难:Mybatis的一 ...
- HuangB2ydjm
Hi! 我现在呢还是学生,想想初中的同学都结婚生子了,自己,嗯.(啊!!!) 本科以及硕士读的都是应用统计 在这里请广大网友多多指教了! 如果有机会的话,大家可以一起造轮子 you can catch ...