leetcode-3-Longest Substring Without Repeating Characters
题目: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.
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 }
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相关推荐
- 【贪心】LeetCode 3. Longest Substring Without Repeating Characters
LeetCode 3. Longest Substring Without Repeating Characters Solution1:我的答案 该方法中哈希表记录的是字符出现的次数.标准的贪心算法 ...
- [LeetCode]3.Longest Substring Without Repeating Characters
[题目] Given a string, find the length of the longest substring without repeating characters. For exam ...
- LeetCode:3. Longest Substring Without Repeating Characters
https://leetcode.com/problems/longest-substring-without-repeating-characters/description/ 内容描述: Give ...
- [LeetCode]--3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- leetcode 3. Longest Substring Without Repeating Characters 最长非重复子串的长度 滑动窗口法
题目链接 根据我们之前介绍的滑动窗口法的解法: 滑动窗口法详解 leetcode 438. Find All Anagrams in a String 滑动窗口法 这题,我们不难解决,使用之前的模板. ...
- python刷leetcode_零基础python刷leetcode -- 3. Longest Substring Without Repeating Characters
算法很重要,但是每天也需要学学python,于是就想用python刷leetcode 的算法题,和我一起开始零基础python刷leetcode之旅吧.如有不对的地方,希望指正,万分感谢~~ 题目 最 ...
- leetcode 3. Longest Substring Without Repeating Characters | 3. 无重复字符的最长子串(双指针+滑窗)
题目 https://leetcode.com/problems/longest-substring-without-repeating-characters/ 题解 双指针+滑窗,维护一个 set, ...
- LeetCode 3 Longest Substring Without Repeating Characters 区间,想法 难度:1
https://leetcode.com/problems/longest-substring-without-repeating-characters/ 思路:从某点结束所能取到的最早开头是到目前出 ...
- leetcode 3. Longest Substring Without Repeating Characters
记得这要+1,j = std::max(j,container[s[i]]+1); class Solution { public:int lengthOfLongestSubstring(strin ...
- [LeetCode] 3. Longest Substring Without Repeating Characters 题解
问题描述 输入一个字符串,找到其中最长的不重复子串 例1: 输入:"abcabcbb" 输出:3 解释:最长非重复子串为"abc" 复制代码 例2: 输入:&q ...
最新文章
- C 家族程序设计语言发展史
- html实现照片添加功能,HTML5 Canvas调用手机拍照功能实现图片上传功能(图文详解上篇)...
- 阿里云邀您参加2020年数据湖高峰会议
- jquery 事件,注册 与重复事件处理
- Java执行系统命令策略
- JS总是带有一种神奇的魔力
- Oracle客户端tnsnames.ora连接配置
- !peb和PEB结构
- 数据库sql语句关键词大全(适合老人),基本你能知道这几个关键词,基本就知道该如何写了(超简略)
- less06 引入(importing)
- linux批量解压压缩包
- 计算机专业外出交流方案,公开学院计算机系外出考察方案.doc
- 《匆匆那年》每一集的标题
- 瑞萨RH850 FCL、FDL和EEL库的配置和使用
- 服装企业SPA经营模式解析
- 算法基础(2) | 高精度、前缀和、差分
- ZSD018出货明细表
- 互联网公司的前台让我学 Flutter :)
- Java对接AD/LDAP的常见错误,以及解决办法
- 特别策划|5G最新进展深度解析2022版—全球市场篇(32页附下载)
热门文章
- 【待完善】MongoDB - 数据模型
- react 打包体积过大_解决 webpack 打包文件体积过大
- java web调用exe文件_从网页WEB上调用本地应用程序(java)
- dhcp协议_记录一次DHCP协议的学习过程
- AcWing 870. 约数个数(唯一分解+组合数)
- python 中指针_【系列】Python源码剖析(base 2.7.18)Note之初见“对象”
- Semaphore(多资源多线程)
- 不规则物体形状匹配综述
- python3学习之元组
- HDU2006 求奇数的乘积