Longest Substring Without Repeating Characters
2015-07-03 14:37
162 查看
public class Solution { /** * 思想:维护两个下标start,end,start从0开始,end从1开始 * 两个指针一直向后遍历,先截取子串,并记录下最大子串的长度,然后再进行比较, * 移动start以及end指针,其中end指针一直加,start指针只有当出现相同字符才往前移动。 * 注:substring截取子串时是半开闭区间的[start,end),保证了每次截取的子串都没有包含重复的字符 */ public int lengthOfLongestSubstring(String s) { if(s == null || s.length() == 0){ return 0; } if(s.length() == 1){ return 1; } int start =0; int end = 0; String subStr = ""; int max = 0; for(int i = 0; i < s.length(); i++){ end = i + 1; subStr = s.substring(start,end); max = max < subStr.length() ? subStr.length() : max; if(end == s.length()){ break; } int index = subStr.indexOf(s.charAt(end)); if(index !=-1){ start += (index + 1); } } return max; } }
相关文章推荐
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- leetcode_linearList
- leetcode_linearList02
- LeetCode[Day 1] Two Sum 题解
- LeetCode[Day 2] Median of Two Sorted Arrays 题解
- LeetCode[Day 3] Longest Substring Without... 题解
- LeetCode [Day 4] Add Two Numbers 题解
- LeetCode [Day 5] Longest Palindromic Substring 题解
- LeetCode [Day 6] ZigZag Conversion 题解