3Longest Substring Without Repeating Characters
来源:互联网 发布:加工中心侧铣头编程 编辑:程序博客网 时间:2024/05/14 02:59
题目链接:https://leetcode.com/problems/longest-substring-without-repeating-characters/
题目:
Given a string, find the length of the longest substring without repeating characters. For 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.
解题思路:
- 建立一个 HashMap 用于存储字符串中的每一个字符,以及该字符的下标
- 指针 start,指向当前未重复字符串的起始字符
- 指针 end,指向当前未重复字符串的最后一个字符的后继
- 维护一个当前未重复字符串的长度(局部最优) localmax
- 维护一个到目前为止,出现过的未重复字符串的最大长度(全局最优)globalmax
- 检查 HashMap 中下标大于等于 start(下标小于 start 的字符已不在当前维护的未重复字符串中)的字符中是否已存在 end 所指向的字符,如果没有,就将该字符放入 map 中。localmax 加一
- 否则,比较当前全局最优和局部最优,将全局最优进行更新。然后,将 start 变为 map 中已重复字符的下一个字符(start 变为重复字符的后继),再将当前字符放入 map 中
- 遍历完整个字符串后,需要再更新一次全局最优
public class Solution { public int lengthOfLongestSubstring(String s) { if(s == null || s.length() == 0) return 0; if(s.length() == 1) return 1; HashMap<Character, Integer> map = new HashMap(); map.put(s.charAt(0), 0); int localMax = 1; int globalMax = 0; int start = 0; int end = 1; while(end < s.length()) { Character c = s.charAt(end); if(map.containsKey(c) && map.get(c) >= start) { globalMax = Math.max(globalMax, localMax); start = map.get(c) + 1; localMax = end - start + 1; map.put(c, end); } else { localMax ++; map.put(c, end); } end ++; } globalMax = Math.max(globalMax, localMax); return globalMax; }}
981 / 981 test cases passed.Status: AcceptedRuntime: 388 ms
另一个类似的方法
参考链接:http://blog.csdn.net/linhuanmars/article/details/19949159
public class Solution { public int lengthOfLongestSubstring(String s) { if(s==null || s.length()==0) return 0; HashSet<Character> set = new HashSet<Character>(); int max = 0; int walker = 0; int runner = 0; while(runner<s.length()) { if(set.contains(s.charAt(runner))) { if(max<runner-walker) { max = runner-walker; } while(s.charAt(walker)!=s.charAt(runner)) { set.remove(s.charAt(walker)); walker++; } walker++; } else { set.add(s.charAt(runner)); } runner++; } max = Math.max(max,runner-walker); return max; }}
981 / 981 test cases passed.Status: AcceptedRuntime: 388 ms
0 0
- LeetCode 3 - Longest Substring Without Repeating Characters
- LeetCode 3: Longest Substring Without Repeating Characters
- leetcode-3:Longest Substring Without Repeating Characters
- LeetCode(3) Longest Substring Without Repeating Characters
- leetcode 3LONGEST SUBSTRING WITHOUT REPEATING CHARACTERS
- Leetcode【3】:Longest Substring Without Repeating Characters
- [leetcode 3] Longest Substring Without Repeating Characters
- [Leetcode] 3 - Longest Substring Without Repeating Characters
- LeetCode 3:《Longest Substring Without Repeating Characters》
- [leetcode 3] Longest Substring Without Repeating Characters
- 3、Longest Substring Without Repeating Characters
- leetrcode 3 Longest Substring Without Repeating Characters
- [leetcode] 3 Longest Substring Without Repeating Characters
- [Leetcode]3Longest Substring Without Repeating Characters
- leetcode 3 Longest Substring Without Repeating Characters
- 3 Longest Substring Without Repeating Characters
- LeetCode #3 Longest Substring Without Repeating Characters
- 3、Longest Substring Without Repeating Characters
- CentOS修改MySql数据库目录datadir
- java 从控制台输入密码
- 请问LINUX下,怎么检查硬件损坏的情况?
- BZOJ 1467 Pku3243 clever Y EXBSGS
- Excel导入TestLink
- 3Longest Substring Without Repeating Characters
- 利用Reveal 分析 iOS APP UI
- HDOJ--1596--find the safest road
- const
- Mac 使用adb shell 命令
- [Play] route配置注意
- 可下载全球历史卫星,无偏移、高精度-谷歌地球卫片
- 关于SearchView的一些小细节
- mac电脑jdk版本切换