首先弄清楚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寻找不重复的最大子串的更多相关文章

  1. LeetCode——Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  2. 【LeetCode】3 、Longest Substring Without Repeating Characters

    题目等级:Medium 题目描述:   Given a string, find the length of the longest substring without repeating chara ...

  3. LeetCode题解 || Longest Substring Without Repeating Characters (O(n)算法)问题

    problem: Given a string, find the length of the longest substring without repeating characters. For ...

  4. LeetCode[3] Longest Substring Without Repeating Characters

    题目描述 Given a string, find the length of the longest substring without repeating characters. For exam ...

  5. [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  6. Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

  7. 3. Longest Substring Without Repeating Characters(c++) 15ms

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

  8. 【leetcode】Longest Substring Without Repeating Characters

    题目描述: Given a string, find the length of the longest substring without repeating characters. For exa ...

  9. Longest Substring Without Repeating Characters(C语言实现)

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

随机推荐

  1. ScheduledThreadPoolExecutor源码分析-你知道定时线程池是如何实现延迟执行和周期执行的吗?

    Java版本:8u261. 1 简介 ScheduledThreadPoolExecutor即定时线程池,是用来执行延迟任务或周期性任务的.相比于Timer的单线程,定时线程池在遇到任务抛出异常的时候 ...

  2. 老猿学5G:融合计费场景的离线计费会话的Nchf_OfflineOnlyCharging_Create创建操作

    ☞ ░ 前往老猿Python博文目录 ░ 一.Nchf_OfflineOnlyCharging_Create消息交互流程 Nchf_OfflineOnlyCharging_Create服务化操作请求是 ...

  3. Python使用import导入模块时报ValueError: source code string cannot contain null bytes的解决方案

    老猿在导入一个Python模块时报错: >>> import restartnet.py Traceback (most recent call last): File " ...

  4. PHP代码审计分段讲解(6)

    14 intval函数四舍五入 <?php if($_GET[id]) { mysql_connect(SAE_MYSQL_HOST_M . ':' . SAE_MYSQL_PORT,SAE_M ...

  5. 本地代码上传到github

    一,注册Github账号 1.先注册一个账号,注册地址:https://github.com/ 2.登录后,点击start a project 3.创建一个repository name,输入框随便取 ...

  6. 公司只提供签名服务,不提供证书文件,如何打包Electron应用

    需求 稍微正规点的公司,都要为自己开发的软件做代码签名,如下图所示 代码签名的主要目的是为了确保软件的来源(这个软件是由谁生产的)和软件的内容不被篡改 一个软件公司可能有很多团队,很多开发者,开发不同 ...

  7. 密码学系列之:明文攻击和Bletchley Park

    目录 简介 crib和明文攻击 布莱奇利公园(Bletchley Park) 简介 明文攻击就是指已经知道了部分明文和它对应的加密后的字段,从而可以推测出使用的加密手段或者密码本.明文攻击这个故事还要 ...

  8. AcWing 204. 表达整数的奇怪方式 / Strange Way To Express Integers

    我作为一个初中蒟蒻,听y大视频听了5遍还不懂,快哭了.然后终于(好像)搞懂,写成题解加深一下记忆... 将式子等价转换 对于每两个式子(我们考虑将其合并): \(x \equiv a_1 \%\ m_ ...

  9. 6个JS特效教程,学完即精通

    6个JS特效教程,学完即精通 JavaScript特效教程,学完你就能写任何特效.本课程将JavaScript.BOM.DOM.jQuery和Ajax课程中的各种网页特效提取出了再进行汇总.内容涵盖了 ...

  10. 在Linux下下载RPM包

    在Linux下下载RPM包,但是不安装 在工作中经常会遇到离线安装RPM包的情况,下面是下载RPM包的方法 # 使用yum下载RPM包 yum -y install --downloadonly &l ...