Algorithm:Longest Substring Without Repeating Characters之Java实现(含最优解)

题目

 

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: "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.
Example 3:

Input: "pwwkew"
Output: 3
Explanation: 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.

找出字符串中最长的不含有重复字符的子串长度。

解法概览

解法一

最好执行用时: 40 ms, 在Longest Substring Without Repeating Characters的Java提交中击败了83.17% 的用户。

最差执行用时: 84 ms, 在Longest Substring Without Repeating Characters的Java提交中击败了29.69% 的用户。

复杂度分析:
       时间复杂度:O(n)
      空间复杂度:O(min(n, m))O(min(n,m)),我们需要 O(k)O(k) 的空间来检查子字符串中是否有重复字符,其中 kk 表示 Set 的大小。而 Set 的大小取决于字符串 nn 的大小以及字符集/字母 mm 的大小。 

public int lengthOfLongestSubstring(String s) {
		//记住子字符串与长度的映射关系
		Map<Integer,String> strLengthStrMap = new HashMap<>();
		//记住已经存在的字符与位置的映射关系
		Map<Character,Integer> tempCharMap = new HashMap<>();
		if(s == null || s.length()  == 0) {
			return 0;
		}
		int max = 0;
		char[] strArray = s.toCharArray();
		String temp = "";
		Character current = null;
		int length = 0;
		int before = 0;
		int point = 0;
		for(int i = 0; i < strArray.length; i++) {
			current = strArray[i];
			if(tempCharMap.containsKey(current)) {
				//如果包含重复字符,把之前的字符存下来
				length = temp.length();
				if(length > max) {
					max = length;
				}
				if(!strLengthStrMap.containsKey(length)) {
					strLengthStrMap.put(length, temp);
				}
				
				//然后重新计算
				before = tempCharMap.get(current);
				if(i - before == 1) {
					temp = "" + current;
					tempCharMap.clear();
					point = i;
				}else if(before < point){
					temp = s.substring(point+1, i+1);
				}else {
					if(i < strArray.length - 1) {
						temp = s.substring(before+1, i+1);
					}else {
						temp = s.substring(before+1, i);
					}
					point = before;
				}
				tempCharMap.put(current, i);
			}else {
				temp = temp + current;
				tempCharMap.put(current, i);
			}
		}
		
		//如果比最大长度更大,则替换最大值
		//针对空格字符串情况以及字符串到最后情况
		length = temp.length();
		if(length > max) {
			max = length;
		}
		return max;
	}

解法二 最优解

最差执行用时: 50 ms, 在Longest Substring Without Repeating Characters的Java提交中击败了70.90% 的用户。

最好执行用时: 27 ms, 在Longest Substring Without Repeating Characters的Java提交中击败了97.36% 的用户。

官方提供。

复杂度分析:

  • 时间复杂度:O(n)O(n),索引 jj 将会迭代 nn 次。

  • 空间复杂度(HashMap):O(min(m, n))O(min(m,n)),与之前的方法相同。

  • 空间复杂度(Table):O(m)O(m),mm 是字符集的大小。

public int lengthOfLongestSubstring(String s) {
        int n = s.length(), ans = 0;
        int[] index = new int[128]; // current index of character
        // try to extend the range [i, j]
        for (int j = 0, i = 0; j < n; j++) {
            i = Math.max(index[s.charAt(j)], i);
            ans = Math.max(ans, j - i + 1);
            index[s.charAt(j)] = j + 1;
        }
        return ans;
    }

 

文章结束。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值