Java for LeetCode 003 Longest Substring Without Repeating Characters

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.

解题思路一

由于题目给个tag为HashTable和Two Pointers

因此,我们可以定义一个HashTable,和两个Pointers,index1和index2,首先将String中元素不断添加进去,如果发现重复,则删除重复的元素和它之前的元素,它们在String中的位置分别用index1和index2进行标记,最后找到HashTable曾经的最大规模。这种思路的时间复杂度为O(n)代码如下

 1 public class Solution {
 2 static public int lengthOfLongestSubstring(String s) {
 3         char[] char1 = s.toCharArray();
 4         int longestLength = 0;
 5         int startIndex = 0;
 6         int lastIndex = 0;
 7         HashMap<Character, Integer> hashMap = new HashMap<Character, Integer>();
 8         for (int i = 0; i < char1.length; i++) {
 9             if (hashMap.containsKey(char1[i])) {
10                 lastIndex = hashMap.get(char1[i]);
11                 if (longestLength < (i - startIndex)) {
12                     longestLength = i - startIndex;
13                 }
14                 for (int j = startIndex; j <= lastIndex; j++) {
15                     hashMap.remove(char1[j]);
16                 }
17                 startIndex = lastIndex+1;
18             }
19             hashMap.put(char1[i], i);
20         }
21         if(longestLength<char1.length-startIndex)
22             longestLength=char1.length-startIndex;
23         return longestLength;
24     }
25 }

但是,提交之后显示Time Limit Exceeded,看来时间复杂度还是太高,究其原因,在于中间有两个for循环,第二个for循环每进行一次删除都必须遍历一边,因此时间开销必然很大。

思路二:

其实每次添加过后,我们不一定要在HashMap中删除重复的元素,我们可以用一个指针记录需要删除元素的位置,如果出现重复元素,就把pointer置为重复元素最后一次出现的位置+1即可,判断之前最大的substring的长度和本次产生的substring长度的大小。之后将本元素添加进HashMap里面。当然,最后还需要判断下最后一次获得的子串的长度和之前长度的比较。

Java代码如下

public class Solution {
    	static public int lengthOfLongestSubstring(String s) {
		Map<Character, Integer> hashMap = new HashMap<Character, Integer>();
		int length = 0;
		int pointer = 0;
		for (int i = 0; i < s.length(); i++) {
			if (hashMap.containsKey(s.charAt(i))&& hashMap.get(s.charAt(i)) >= pointer) {
				length = Math.max(i-pointer, length);
				pointer = hashMap.get(s.charAt(i)) + 1;
			}
			hashMap.put(s.charAt(i), i);
		}
		return Math.max(s.length() - pointer, length);
	}
}
时间: 2024-10-21 12:01:01

Java for LeetCode 003 Longest Substring Without Repeating Characters的相关文章

[LeetCode] 003. Longest Substring Without Repeating Characters (Medium) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 003.Longest_Substring_Without_Repeating_Characters (Medium) 链接: 题目:https://oj.leetcode.com/problems/Longest-Substring-Without-Repeating-Characters/ 代码(github)

【LeetCode】003 Longest Substring Without Repeating Characters

题目:LeetCode 003 Longest Substring Without Repeating Characters 题意:给一个字符串,找到一个没有重复字符的最长子串. 样例:”bbbbb” 返回”b”的长度1:”abcabcbb”返回”abc”的长度3. 思路: 动态规划.dp[i]表示以第i个字符为结尾的无重复字符的子串的最长的长度,需要一个辅助数组idx[s[i]]记录字符当前字符s[i]上一次出现的位置,如果未出现过则为-1.所以得到如下的递推公式: 另外由于不确定字符串长度的

【leetcode】 Longest Substring Without Repeating Characters

题目: 给定一个字符串,返回该串没有重复字符的最长子串. 分析: 1)子串:子串要求是连续的. 2)无重复,出现重复就断了,必须从新的位置开始.而新的位置就是重复字符第一次出现位置的下一个位置. 3)整个串可能没有一处重复. 那么,为了找出当前访问的字符是否出现过,要怎么做呢?当然是hash,O(1)的时间,而且既然是字符, 定义个255的hash table 就可以了,hash table 中的元素为相应字符在s中出现的位置.初始化为-1,表示都没有出现. 我们另外定义一个start 和end

leetcode -day21 Longest Substring Without Repeating Characters

 1.Longest Substring Without Repeating Characters 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

LeetCode-Algorithms #003 Longest Substring Without Repeating Characters, Database #177 Nth Highest Salary

LeetCode-Algorithms #003 Longest Substring Without Repeating Characters 对于给定的字符串, 找出其每个字符都不重复的子串中最长的那个, 并返回该子串的长度: 想法还是遍历: 1 class Solution { 2 public int lengthOfLongestSubstring(String s) { 3 // 如果s是null或空串, 就直接返回0 4 if (s == null || "".equals

[LeetCode] 5. Longest Substring Without Repeating Characters 最长回文子串

[LeetCode] 5. Longest Substring Without Repeating Characters Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. Example 1: Input: "babad" Output: "bab" Note: "aba&qu

LeetCode 003. Longest Substring Without Repeating

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 subst

Java [leetcode 3] Longest Substring Without Repeating Characters

问题描述: 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

Leetcode 3 Longest Substring Without Repeating Characters. (最长无重复字符子串) (滑动窗口, 双指针)

目录 问题描述 例子 方法 Leetcode 3 问题描述 Given a string, find the length of the longest substring without repeating characters. 例子 Example 1: Input: "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3. Example 2: Input: &q