leetcode-3-Longest Substring Without Repeating Characters
Posted pathjh
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);
这句话很好理解,就是更新过程中的最大值
真的是很巧妙!
以上是关于leetcode-3-Longest Substring Without Repeating Characters的主要内容,如果未能解决你的问题,请参考以下文章
3. 没有重复字母的最长子串 [leetcode 3: Longest Substring Without Repeating Characters]
3. 没有重复字母的最长子串 [leetcode 3: Longest Substring Without Repeating Characters]
leetcode 3. Longest Substring Without Repeating Characters (Python版)
LeetCode - 3 - Longest Substring Without Repeating Characters