(DP)3.Longest Substring Without Repeating Characters
2016-04-11 02:43
417 查看
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 class Solution { public int lengthOfLongestSubstring(String s) { if (s.length() == 0) return 0; String[] dp = new String[s.length()]; dp[0] = s.substring(0, 1); int index = 0; for (int i = 1; i < s.length(); i++) { if (s.charAt(i) == s.charAt(i - 1)) dp[i] = s.substring(i - 1, i); else { index = dp[i - 1].indexOf(s.charAt(i)); if (index == -1) dp[i] = dp[i - 1] + s.charAt(i); else dp[i] = dp[i - 1].substring(index+1) +s.charAt(i); } } index = 0; for (int i = 0; i < s.length(); i++) { if (dp[i].length() > index) index = dp[i].length(); } return index; } }
相关文章推荐
- 使用Eclipse的预备好习惯
- Win2008搭建Web和FTP服务
- 初识PHP(四)PDO对象配置于使用
- java HashMap源码剖析
- 使用Fabric部署网站应用
- Android am命令使用
- GDUT Krito的讨伐(bfs&&优先队列)
- 利用ARIMA模型对标普500指数进行预测
- we job IT网设计
- VB6 加载控件Windows Common Controls 6.0(mscomctl.ocx)失败提示“对象库未注册”
- lwIP缓冲区重新分配函数pbuf_realloc说明
- JAVA时间戳与日期转换
- Forbidden You don't have permission to access /php/ on this server.
- [RxJS] Creation operator: of()
- LeetCode 268. Missing Number(缺失数字)
- LeetCode 169. Majority Element
- easyui combo+pagination 图标选择器
- HDOJ 1395 2^x mod n = 1
- HDOJ 1395 2^x mod n = 1
- Unity Shaders and Effects Cookbook (3-4) 使用高光贴图