3. Longest Substring Without Repeating Characters
2016-03-10 14:36
393 查看
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.
of 1.
public class Solution { public int lengthOfLongestSubstring(String s) { int max = 0, i =0 , j = 0,index; int[] hm = new int[128]; //ASCII字符表128个,用来表示某字符当前位置 while(j<s.length()){ //进行一一遍历 if(hm[s.charAt(j)] == 0){ //如果没有出现过 hm[s.charAt(j)] = j+1; //标记为当前位置 j++; if((j-i) > max) //j-i为此次寻找的字符串的长度,取最大值 max = j-i; } else { //如果出现过 index = hm[s.charAt(j)]; //index为当前位置 hm[s.charAt(j)] = 0; if (i < index) i = index; } } return max; } }
相关文章推荐
- 154.View the Exhibit and evaluate structures of the SALES, PRODUCTS, and COSTS tables.
- Exception:a different object with the same identifier value was already associated with the session
- 第一篇文章--学习小感欢迎交流
- 动态的修改xml中的drawable文件的solid颜色
- 如何创建servlet3.0的项目?
- 用户手势检测-GestureDetector
- 面试注意
- cocos2dx ListView 动态加载(代码)
- 使用xcode自带的git
- TF/IDF(term frequency/inverse document frequency)
- Python求索之路4——模块
- 列表下拉刷新,上拉加载更多
- 递归
- log4net使用
- HDU 4565 So Easy! 矩阵快速幂
- tomcat启动过程报the JDBC Driver has been forcibly unregistered问题的修复过程
- Git使用(一)
- VS2008试用期过期,没有填写序列号的地方
- Linux 容器技术的三大致命隐忧
- webview 中js交互 代码混淆后失效