题目:Longest Substring Without Repeating Characters

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

Examples:

Given "abcabcbb", the answer is "abc", which the length is 3.

Given "bbbbb", the answer is "b", with the length of 1.

Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.


思路:这个题之前在nowcoder上做过。要求找出不重复的最长子串,我的方法是构造一个滑动窗口记录最长不重复子串的左右位置,左窗口left最初为0,用max来记录过程中的最长不重复子串的长度,只要当前字符未在之前出现过,则left保持和之前一样的值,遍历指针i就一直往后移,则当前的最长长度就为max=max(max,i-left+1),如果当前字符在前面字符重复了,则放弃前面那个字符的位置,即让left=前面那个重复的字符的位置的下一个位置(即放弃前面那个字符选这个字符);对于字符的判重,我的做法是利用HashMap来记录,在遍历整个字符串的同时,将首次出现的字符放入HashMap中,其同样字符再次出现时,便可以判重了,对于重复了的字符由于要将前面的那个舍去,所以,继续将其放入HashMap中
实现代码如下:
 1 import java.util.HashMap;
 2 class Solution {
 3     public int lengthOfLongestSubstring(String s) {
 4         if(s==null) return 0;
 5         HashMap<Character,Integer>map=new HashMap<>();
 6         int left=0;//左窗口最初位置
 7         int max=0;//全局最长长度
 8         for(int i=0;i<s.length();i++){
 9           char c=s.charAt(i);
10           left=Math.max(left,map.containsKey(c)?map.get(c)+1:0);
11           max=Math.max(max,i-left+1);
12           map.put(c,i);
13         }
14         return max;
15     }
16
17     }

显然,我这种算法由于额外借用HashMap,所以实现速度明显不高,学习了下排名第2的算法
 1 class Solution {
 2     public int lengthOfLongestSubstring(String s) {
 3         int n = s.length(), ans = 0;
 4         int[] index = new int[128];
 5         for(int j=0, i=0; j<n; j++){
 6             i = Math.max(index[s.charAt(j)], i);
 7             ans = Math.max(ans, j-i+1);
 8             index[s.charAt(j)] = j+1;
 9         }
10         return ans;
11     }
12 }

来看一下他的这种算法,他是利用了类似于桶排序的思路来记录每个字符的位置,实现方法很巧妙

int[] index = new int[128];
index[s.charAt(j)] = j+1;

用index数组来记录每个字符后面的位置,数组初始化为0,每遍历一个字符就将该字符后面的位置记录下来,只要后面该字符再次出现,则数组中该字符所对应的值就意为放弃前面这个字符后的下一个位置

i = Math.max(index[s.charAt(j)], i);

这里的i就相当于我的方法里的左窗口left

当字符未重复时,index数组初始为0,此时i=i,即左窗口不变化

当字符有重复时i=index[s.charAt(j)],此时的i就相当于放弃了前一个重复字符,选后一个

ans = Math.max(ans, j-i+1);

这句话很好理解,就是更新过程中的最大值

真的是很巧妙!

转载于:https://www.cnblogs.com/pathjh/p/9094593.html

leetcode-3-Longest Substring Without Repeating Characters相关推荐

  1. 【贪心】LeetCode 3. Longest Substring Without Repeating Characters

    LeetCode 3. Longest Substring Without Repeating Characters Solution1:我的答案 该方法中哈希表记录的是字符出现的次数.标准的贪心算法 ...

  2. [LeetCode]3.Longest Substring Without Repeating Characters

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

  3. LeetCode:3. Longest Substring Without Repeating Characters

    https://leetcode.com/problems/longest-substring-without-repeating-characters/description/ 内容描述: Give ...

  4. [LeetCode]--3. Longest Substring Without Repeating Characters

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

  5. leetcode 3. Longest Substring Without Repeating Characters 最长非重复子串的长度 滑动窗口法

    题目链接 根据我们之前介绍的滑动窗口法的解法: 滑动窗口法详解 leetcode 438. Find All Anagrams in a String 滑动窗口法 这题,我们不难解决,使用之前的模板. ...

  6. python刷leetcode_零基础python刷leetcode -- 3. Longest Substring Without Repeating Characters

    算法很重要,但是每天也需要学学python,于是就想用python刷leetcode 的算法题,和我一起开始零基础python刷leetcode之旅吧.如有不对的地方,希望指正,万分感谢~~ 题目 最 ...

  7. leetcode 3. Longest Substring Without Repeating Characters | 3. 无重复字符的最长子串(双指针+滑窗)

    题目 https://leetcode.com/problems/longest-substring-without-repeating-characters/ 题解 双指针+滑窗,维护一个 set, ...

  8. LeetCode 3 Longest Substring Without Repeating Characters 区间,想法 难度:1

    https://leetcode.com/problems/longest-substring-without-repeating-characters/ 思路:从某点结束所能取到的最早开头是到目前出 ...

  9. leetcode 3. Longest Substring Without Repeating Characters

    记得这要+1,j = std::max(j,container[s[i]]+1); class Solution { public:int lengthOfLongestSubstring(strin ...

  10. [LeetCode] 3. Longest Substring Without Repeating Characters 题解

    问题描述 输入一个字符串,找到其中最长的不重复子串 例1: 输入:"abcabcbb" 输出:3 解释:最长非重复子串为"abc" 复制代码 例2: 输入:&q ...

最新文章

  1. C 家族程序设计语言发展史
  2. html实现照片添加功能,HTML5 Canvas调用手机拍照功能实现图片上传功能(图文详解上篇)...
  3. 阿里云邀您参加2020年数据湖高峰会议
  4. jquery 事件,注册 与重复事件处理
  5. Java执行系统命令策略
  6. JS总是带有一种神奇的魔力
  7. Oracle客户端tnsnames.ora连接配置
  8. !peb和PEB结构
  9. 数据库sql语句关键词大全(适合老人),基本你能知道这几个关键词,基本就知道该如何写了(超简略)
  10. less06 引入(importing)
  11. linux批量解压压缩包
  12. 计算机专业外出交流方案,公开学院计算机系外出考察方案.doc
  13. 《匆匆那年》每一集的标题
  14. 瑞萨RH850 FCL、FDL和EEL库的配置和使用
  15. 服装企业SPA经营模式解析
  16. 算法基础(2) | 高精度、前缀和、差分
  17. ZSD018出货明细表
  18. 互联网公司的前台让我学 Flutter :)
  19. Java对接AD/LDAP的常见错误,以及解决办法
  20. 特别策划|5G最新进展深度解析2022版—全球市场篇(32页附下载)

热门文章

  1. 【待完善】MongoDB - 数据模型
  2. react 打包体积过大_解决 webpack 打包文件体积过大
  3. java web调用exe文件_从网页WEB上调用本地应用程序(java)
  4. dhcp协议_记录一次DHCP协议的学习过程
  5. AcWing 870. 约数个数(唯一分解+组合数)
  6. python 中指针_【系列】Python源码剖析(base 2.7.18)Note之初见“对象”
  7. Semaphore(多资源多线程)
  8. 不规则物体形状匹配综述
  9. python3学习之元组
  10. HDU2006 求奇数的乘积