LeetCode: Longest Substring Without Repeating Characters
2014-07-03 22:24
435 查看
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.
Solution:
Solution:
public class Solution { public int lengthOfLongestSubstring(String s) { int maxLen = 0; if(s.length() == 1) return 1; else { int l = 0; int r = 0; int[] set = new int[256]; Arrays.fill(set,-1); for(int i = 0; i < s.length(); i++){ int len = 0; if(-1 == set[s.charAt(i)] || set[s.charAt(i)] < l){ r = i; len = r -l+1; set[s.charAt(i)] = i; }else { l = set[s.charAt(i)] + 1; r = i; set[s.charAt(i)] = i; } maxLen = Math.max(maxLen,len); } return maxLen; } } }
相关文章推荐
- Leetcode03---Longest Substring Without Repeating Characters
- 【leetcode】Longest Substring Without Repeating Characters
- [leetcode]Longest Substring Without Repeating Characters
- LeetCode力扣之Longest Substring Without Repeating Characters
- [LeetCode]#3 Longest Substring Without Repeating Characters
- leetcode: Longest Substring Without Repeating Characters
- [LeetCode]Longest Substring Without Repeating Characters
- Leetcode3:Longest Substring Without Repeating Characters@Python
- LeetCode 3:Longest Substring Without Repeating Characters
- leetcode--Longest Substring Without Repeating Characters
- LeetCode刷题(C++)——Longest Substring Without Repeating Characters(Medium)
- Leetcode代码学习周记——Longest Substring Without Repeating Characters
- LeetCode解题-#3-Longest Substring Without Repeating Characters
- LeetCode-Longest Substring Without Repeating Characters
- Leetcode 3: Longest Substring Without Repeating Characters
- [LeetCode] Longest Substring Without Repeating Characters 解题报告
- LeetCode3:Longest Substring Without Repeating Characters
- LeetCode 03: Longest Substring Without Repeating Characters
- leetcode 3 Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters -leetcode