LeetCode:Longest Substring Without Repeating Characters
2015-08-13 13:24
381 查看
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.
public static int lengthOfLongestSubstring(String s) { if(s == null || s.trim().length() == 0){ return 0; } s = s.trim(); int len = s.length(); //char类型8位最多可表示256种字符 int[] arrayOfIndex = new int[256]; Arrays.fill(arrayOfIndex,-1); int bIndex = 0, tmpLen = 0, maxLen = 0; char c; for(int i=0; i<len; i++){ c = s.charAt(i); //发现重复字符,保证重复字符在当前子字符串内 if(arrayOfIndex[c] >= bIndex){ maxLen = maxLen > tmpLen ? maxLen : tmpLen; //重置子字符串的起始位置,该位置有可能在上一个子字符串的内部 bIndex = arrayOfIndex[c]+1; //重置子字符串的长度 tmpLen = i-arrayOfIndex[c]-1; } tmpLen++; //记录当前字符在字符串中的位置 arrayOfIndex[c] = i; } return maxLen > tmpLen ? maxLen : tmpLen; }
相关文章推荐
- 【VR】Leap Motion 官网文档 MagneticPinch (磁性捏取)
- 异常总结
- Spring Data JPA - “No Property Found for Type” Exception
- AVL树的构造实现
- Unity 场景异步加载
- [LeetCode155] Min Stack
- jQuery简要dom操作
- T-SQL CROSS APPLY、MERGE
- Mac 下Versions的 svn无法上传 .a 文件的问题
- 手机短信验证接口推荐
- spark向量、矩阵类型
- XShell安装和配置(系统为虚拟机中的centos7)
- CentOS7.1配置源
- iOS利用Runtime自定义控制器POP手势动画(经典)
- 字节序与字节对齐
- [LeetCode156] Binary Tree Upside Down
- maven jetty
- Android如何快速打出100个渠道apk
- [经典算法题]寻找数组中第K大的数的方法总结
- 数据恢复陶工西数500GWD5000AAKX-08U6AA0摔敲盘不认紫色线圈磁头开盘恢复数据