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 ...
随机推荐
- 网络最大流 Dinic算法
前言 看到网上好多都用的链式前向星,就我在用 \(vector\) 邻接表-- 定义 先来介绍一些相关的定义.(个人理解) 网络 一个网络是一张带权的有向图 \(G=(V,E)\) ,其中每任意一条边 ...
- python核心高级学习总结4-------python实现进程通信
Queue的使用 Queue在数据结构中也接触过,在操作系统里面叫消息队列. 使用示例 # coding=utf-8 from multiprocessing import Queue q = Que ...
- 深入浅出让你理解什么是LLVM
什么是LLVM 转载自https://www.jianshu.com/p/1367dad95445 LLVM项目是模块化.可重用的编译器以及工具链技术的集合. 美国计算机协会 (ACM) 将其2012 ...
- 第 7篇 Scrum 冲刺博客
一.站立式会议 1.站立式会议照片 2.昨天已完成的工作 对职工的查询 3.今天计划完成的工作 继续与同学对接,争取早日完成项目的整个流程 初步对数据库筛选 4.工作中遇到的困难 ①有同学不知道如何远 ...
- mock.js 和easy-mock使用
mock.js 1.项目中引入mock.js <script src="../static/js/mock.js" type="text/javascript&qu ...
- bootstrap 扩展参数
后台接受的参数形式 前端加载bootstrap时做的处理
- 题解-CF755G PolandBall and Many Other Balls
题面 CF755G PolandBall and Many Other Balls 给定 \(n\) 和 \(m\).有一排 \(n\) 个球,求对于每个 \(1\le k\le m\),选出 \(k ...
- 题解 CF1437G Death DBMS
这题感觉不是很难,但是既然放在 \(\texttt{EDU}\) 的 \(\texttt{G}\) 题,那么还是写写题解吧. \(\texttt{Solution}\) 首先看到 "子串&q ...
- NOI Online 题解
T1 对\(t_i = 1\)的边,将\(u_i, v_i\)连一条边权为\(1\)的边.否则连一条边权为\(0\)的边. 对于每一个连通块,若图中不存在一条边权之和为奇数的圈,则可以将这个连通块二染 ...
- 题解-CF1437E Make It Increasing
题面 CF1437E Make It Increasing 给 \(n\) 个数 \(a_i\),固定 \(k\) 个下标 \(b_i\),求只修改不在 \(b_i\) 中的下标的值使 \(a_i\) ...