摘要:哈希表法双指针法只有当也就是时上面的循环才会结束,,跳过这个之前的重复字符再将置为
Problem
Given a string, find the length of the longest substring without repeating characters.
ExampleFor example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3.
For "bbbbb" the longest substring is "b", with the length of 1.
Note Solution1. 哈希表法
HashMap Method I
public class Solution { public int lengthOfLongestSubstring(String s) { HashMapmap = new HashMap (); int count = 0, start = 0; for (int i = 0; i < s.length(); i++) { if (map.containsKey(s.charAt(i))) { int index = map.get(s.charAt(i)); for (int j = start; j <= index; j++) { map.remove(s.charAt(j)); } start = index + 1; } map.put(s.charAt(i), i); count = Math.max(count, i - start + 1); } return count; } }
HashMap Method II
public class Solution { public int lengthOfLongestSubstring(String s) { if (s == null || s.length() == 0) return 0; HashMapmap = new HashMap<>(); int max = 1, start = 0; for (int i = 0; i < s.length(); i++) { Character cur = s.charAt(i); Integer rep = map.get(cur); if (rep != null && rep >= start) { max = Math.max(max, i-start); start = rep+1; } map.put(cur, i); } return Math.max(s.length()-start, max); } }
2. 双指针法
public class Solution { public int lengthOfLongestSubstring(String s) { int start = 0, end = 0, count = 0; boolean[] mark = new boolean[256]; while (end < s.length()) { if (!mark[s.charAt(end)]) { mark[s.charAt(end)] = true; count = Math.max(count, end-start+1); end++; } else { while (mark[s.charAt(end)]) { mark[s.charAt(start)] = false; start++; } //只有当s.charAt(start) == s.charAt(end) //也就是mark[s.charAt(end)] == false时 //上面的循环才会结束,start++,跳过这个之前的重复字符 //再将mark[s.charAt(end)]置为true mark[s.charAt(end)] = true; end++; } } return count; } }
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/65600.html
摘要:哈希表是最自然的想法。在遍历字符串时,我们先根据哈希表找出该字符上次出现的位置,如果大于等于子字符串首,便更新子字符串首。结束后,将该字符新的位置放入哈希表中。 Longest Substring Without Repeating Characters 最新更新解法:https://yanjia.me/zh/2018/12/... Given a string, find the ...
Problem 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...
摘要:建立数组,存储个字符最近一次出现的位置。首次出现某字符时,其位置标记为,并用无重复字符计数器记录无重复字符的长度,再在更新其最大值。循环完整个字符串后,返回最大值。 Problem Given a string, find the length of the longest substring without repeating characters. Examples: Given ...
摘要:题目详情题目要求输入一个字符串,我们要找出其中不含重复字符的最长子字符串,返回这个最长子字符串的长度。对于字符串中的每一个字符,先判断中是否已经存在这个字符,如果不存在,直接将添加到,如果已存在,则新的字符串就从不含前一个字符的地方开始。 题目详情 Given a string, find the length of the longest substring without repe...
摘要:解题思路本题借助实现。如果字符未出现过,则字符,如果字符出现过,则维护上次出现的遍历的起始点。注意点每次都要更新字符的位置最后返回时,一定要考虑到从到字符串末尾都没有遇到重复字符的情况,所欲需要比较下和的大小。 Longest Substring Without Repeating CharactersGiven a string, find the length of the lon...
阅读 1629·2019-08-30 15:54
阅读 2374·2019-08-30 15:52
阅读 2047·2019-08-29 15:33
阅读 3042·2019-08-28 17:56
阅读 3236·2019-08-26 13:54
阅读 1675·2019-08-26 12:16
阅读 2449·2019-08-26 11:51
阅读 1644·2019-08-26 10:26