3. Longest Substring Without Repeating Characters
2016-04-22 09:18
169 查看
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given
Given
Given
Similar: 159. Longest Substring with At Most Two Distinct 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.
Similar: 159. Longest Substring with At Most Two Distinct Characters
public class Solution { public int lengthOfLongestSubstring(String s) { HashMap<Character, Integer> map = new HashMap<Character, Integer>(); int max = 0; int start=0; for (int i = 0; i < s.length(); i++) { max = max > (i - start) ? max : (i-start); // [start,i), i is not included char c = s.charAt(i); if (map.containsKey(c)) { int j = map.get(c); while (start <= j) { map.remove(s.charAt(start)); start++; } } map.put(c, i); } max = max > (s.length() - start) ? max : (s.length()-start); // As length = [start,i), i is not included return max; } }
相关文章推荐
- Android Scroll 详解
- C#字符串转日期格式
- selenium学习笔记(selenium下载安装)
- 安装IIS后启动tomcat报80端口被占用
- 求从小到大的第1500个丑数
- 关于EF分页查询报错(Count must have a non-negative value.)的解决方案
- 每天laravel-20160723|Application-3
- 在计算机中删除指定文件
- 该如何解释我们的第六感
- JS中的this变量的使用介绍
- cocoapods常见错误
- 利用UIWebView打造一个炫酷的视频背景视图(OC & Swift)
- 剑指offer面试题 二维数组的查找
- iOS Apple Pay详解以及处理支付逻辑(附带Demo)
- 输出数组的全部组合
- 每天laravel-20160723|Application-3
- 关于H5 audio标签的个人使用心得
- 几个常用的正则验证表达式-可以满足一般需求
- mac中显示隐藏文件
- CSS样式的优先级别