leetcode第三题: 输出不包含重复字母的最长子串
2017-02-15 20:34
357 查看
文章搬家至http://www.cnblogs.com/buptl/p/6520655.html
Examples:
Given “abcabcbb”, the answer is “abc”, which the length is 3.
Given “bbbbb”, the answer is “b”, with the length of 1.
Given “pwwkew”, the answer is “wke”, with the length of 3. Note that the answer must be a substring, “pwke” is a subsequence and not a substring.
也就是说给定一个字符串,输出不包含重复字母的最长子串长度。
1. 记录每个字母上一次出现的位置
2. 维持一个从当前位置往前数不包含重复字母的子串,记录这个字串的起止位置start, end
遍历的过程中就是根据相应位置字母是否出现过,以及上次出现的位置,不断更新start, end的过程。
题目
Given a string, find the length of the longest substring without repeating characters.Examples:
Given “abcabcbb”, the answer is “abc”, which the length is 3.
Given “bbbbb”, the answer is “b”, with the length of 1.
Given “pwwkew”, the answer is “wke”, with the length of 3. Note that the answer must be a substring, “pwke” is a subsequence and not a substring.
也就是说给定一个字符串,输出不包含重复字母的最长子串长度。
思路
遍历一次字符串,O(n)复杂度下可以解决。主要思路就是在遍历的过程中1. 记录每个字母上一次出现的位置
2. 维持一个从当前位置往前数不包含重复字母的子串,记录这个字串的起止位置start, end
遍历的过程中就是根据相应位置字母是否出现过,以及上次出现的位置,不断更新start, end的过程。
代码
可以到github上查看: https://github.com/lcy362/Algorithms/tree/master/src/main/java/com/mallow/algorithmimport java.util.HashMap; /** * leetcode 3 * https://leetcode.com/problems/longest-substring-without-repeating-characters/ * Created by lcy on 2017/2/15. */ public class LongestSubstringNotRepeat { public int lengthOfLongestSubstring(String s) { if (s.length() <= 1) { return s.length(); } HashMap<Character, Integer> charPos = new HashMap<>(); char[] chars = s.toCharArray(); int len = 0; int max = 0; int start = 0; int end = 0; for (int i = 0; i < chars.length; i++) { if (charPos.containsKey(chars[i])) { int tempstart = charPos.get(chars[i]) + 1; if (tempstart > start) { start = tempstart; } end++; len = end - start; } else { len++; end++; } charPos.put(chars[i], i); if (len > max) { max = len; } } return max; } public static void main(String args[]) { LongestSubstringNotRepeat l = new LongestSubstringNotRepeat(); System.out.println(l.lengthOfLongestSubstring("abcabcbb")); System.out.println(l.lengthOfLongestSubstring("bbbbb")); System.out.println(l.lengthOfLongestSubstring("pwwkew")); System.out.println(l.lengthOfLongestSubstring("abba")); }
相关文章推荐
- 【LeetCode002-003算法/编程练习C++】---Add Two Numbers||寻找最长无重复字母子串。//第三题之后有空要再想一想…
- leetcode 第三题 最长不重复子串
- LeetCode 3. Longest Substring Without Repeating Characters--不包含重复字符的最长子串长度
- leetcode第三题:求给定字符串中最长无重复子串的长度
- 找出字符串的最长不重复子串,输出长度
- [C++]LeetCode 3: Longest Substring Without Repeating Characters(最长不重复子串)
- 找出字符串最长不重复子串,输出长度
- 【LeetCode】Longest Substring Without Repeating Characters && 【九度】题目1530:最长不重复子串
- 【LeetCode】4. Longest Substring Without Repeating Characters 最长无重复子串
- [leetcode]Longest Substring Without Repeating Characters【最长不重复字符子串】
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串
- [LeetCode]3. Longest Substring Without Repeating Characters寻找最长无重复字符的子串
- 键盘输入一句话,输出这句话中最长的单词,如果最长的出现多次,返回第一个,这句话只包含数字字母和标点
- 求字符串内不包含重复字符的最长子串
- 一个字符串中可能包含a~z中的多个字符,如有重复,求出现次数最多的那个字母及次数,如有多个重复最多的则都输出。
- leetcode-Longest Substring Without Repeating Characters 最长不重复子串
- 【LeetCode题目记录-3】字符串中最长的没有重复字符的子串
- LeetCode-Longest Substring Without Repeating Characters-最长不重复子串-滑动窗口
- leetcode3:不重复的最长子串长度
- LeetCode-3 Longest Substring Without Repeating Characters(最长无重复子串)