首先弄清楚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. python 子进程

    1.线程的介绍(理论部分) 1.1 进程线程分工 我们之前讲运行一个py文件,就是开启了一个进程,在内存中开辟一个进程空间,将必要的数据加载到这个进程空间,然后cpu在去调用这个进程的主线程去执行具体 ...

  2. java集合类(新手也能掌握)

    文章目录 1.集合概述 (1)集合: (2)集合分类: 2.Collection接口 3.List接口 (1)List接口简介 (2)ArrayList集合 (3)LinkedList集合 (4)It ...

  3. 腾讯短信平台ASP接口范例

    疫情后一个小项目要用到腾讯短信平台,因为比较老,用ASP写的,平台没有相应的ASP接口,百度不到,无奈之下自己写了一个,也方便需要的朋友们. 主要代码如下: <!--#include file= ...

  4. 老猿学5G随笔:5G的三大业务场景eMBB、URLLC、mMTC

    5G的三大业务场景eMBB.URLLC.mMTC: eMBB:英文全称Enhanced Mobile Broadband,即增强移动宽带,是利用5G更好的网络覆盖及更高的传输速率来为用户提供更好的上网 ...

  5. Day3 Scrum 冲刺博客

    ·线上会议: 昨天已完成的工作与今天计划完成的工作及工作中遇到的困难: 成员姓名 昨天完成工作 今天计划完成的工作 工作中遇到的困难 纪昂学 创建一个Cell类,用来表示一个小方块 就创建一个Tetr ...

  6. 一种使用 Redis 深度驱动的,为构建轻量级分布式应用程序(Microservices)的工程方案

    Hydra 是一个轻量级的 NodeJS 库,用于构建分布式计算应用程序,比如微服务.我们对轻量级的定义是:轻处理外部复杂性和基础设施依赖 -- 而不是有限的轻处理. Hydra 声称对基础设施的依赖 ...

  7. 「IOI2017」接线 的另类做法

    看到这题,我的第一反应是:这就是一个费用流模型?用模拟费用流的方法? 这应该是可以的,但是我忘记了怎么模拟费用流了IOI不可能考模拟费用流.于是我就想了另外一个方法. 首先我们考虑模拟费用流的模型如下 ...

  8. nginx 静态化合集(去掉index.php目录)

    访问某域名时,去掉index.php目录时达到效果一样 如:www.test1/index.php/test2跟www.test1/test2效果一致 在vhosts.conf中加重写就可以了 loc ...

  9. 传输层-Transport Layer(上):传输层的功能、三次握手与四次握手、最大-最小公平、AIMD加法递增乘法递减

    第六章 传输层-Transport Layer(上) 6.1传输层概述 在之前的几章内容中,我们自底向上的描述了计算机网络的各个层次,还描述了一些处于不同层次下的经典网络协议(如以太网.无线局域网.或 ...

  10. Java8新特性探索之新日期时间库

    一.为什么引入新的日期时间库 Java对日期,日历及时间的处理一直以来都饱受诟病,尤其是它决定将java.util.Date定义为可修改的以及将SimpleDateFormat实现成非线程安全的. 关 ...