LeetCode:Longest Substring Without Repeating Characters

题目:

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.

本人的解决方案:

class Solution {
    public int lengthOfLongestSubstring(String s) {
        int i = 0;
        int j = 0;
        int max_num = 1;
        int num = 1;
        int flag = 0;
        if(s.length() ==0)
            return 0;
        for(;i<s.length();i++)
        {
            num = 1;
            for(j=i+1;j<s.length();j++)
            {
                for(int k =j-1;k>=i;k--){
                    
                
                    if(s.charAt(j) !=s.charAt(k))
                      flag = 0; 
                    else{
                        flag = 1;
                        break;  
                        }
                }
                if(flag == 0)
                    num++;
                if(flag == 1)
                    break;
            }
            if(num>max_num)
                max_num=num;
        }
        return max_num;
    }
}

时间复杂度为O(n^3),算是比较简单的一种解决方法,但并没有考虑到时间复杂度。
类似于官网给出的答案1:

public class Solution {
    public int lengthOfLongestSubstring(String s) {
        int n = s.length();
        int ans = 0;
        for (int i = 0; i < n; i++)
            for (int j = i + 1; j <= n; j++)
                if (allUnique(s, i, j)) ans = Math.max(ans, j - i);
        return ans;
    }

    public boolean allUnique(String s, int start, int end) {
        Set<Character> set = new HashSet<>();
        for (int i = start; i < end; i++) {
            Character ch = s.charAt(i);
            if (set.contains(ch)) return false;
            set.add(ch);
        }
        return true;
    }
}

方法很简单,但是时间复杂度为O(n^3),耗费时间太多。
于是官方给出了一个滑动窗口的实现方案。

HashSet滑动窗口实现方案

通过使用HashSet作为滑动窗口,检查当前字符是否可以在O(1)O(1)中完成。
于是官网给出了第二种解法:

public class Solution {
    public int lengthOfLongestSubstring(String s) {
        int n = s.length();
        Set<Character> set = new HashSet<>();
        int ans = 0, i = 0, j = 0;
        while (i < n && j < n) {
            // try to extend the range [i, j]
            if (!set.contains(s.charAt(j))){
                set.add(s.charAt(j++));
                ans = Math.max(ans, j - i);
            }
            else {
                set.remove(s.charAt(i++));
            }
        }
        return ans;
    }
}

滑动窗口的意思,便是同时考虑字符串的两端,
原O(n^3)的解法为固定字符串左端,将所有的情况全部遍历一遍,
改进算法为从最左端开始判断每个字符,即最初i=j=0,当Hashset中不存在相同字符,令j+1往后遍历,同时记录最大长度,但是当Hashset中存在相同的字符后,将i+1,开始滑动窗口,并且将HashSet中的i所指示的字符去掉。。
在判断的过程中,若增加右端范围后有重复数据,则目前的最大长度即为当前记录的最大长度,需要从重复的字符处重新判断最大值
具体情况如下图
在这里插入图片描述
如上图所示,当ij到达这样的情况后,我们发现目前最大长度为4,无论j如何往后扩张,都会有重复的数据dd存在,所以为了检验整个字符串中是否存在其他的不重复的子字符串,则需要将i的范围缩小,即往右移动。

官网给出的第三种方案如下

优化后的滑动窗口

在我们涉及到第二种方法时,我们很容易想到,既然发现了重复数据之后,在此重复字符以前的字符所组成的字符串的最大不相等长度已经出现了(即为目前所统计到的最大长度)。所以我们在缩小左部范围时,可以直接跳过j以前的字符,重新从i=j开始继续测试。
代码如下

public class Solution {
    public int lengthOfLongestSubstring(String s) {
        int n = s.length(), ans = 0;
        Map<Character, Integer> map = new HashMap<>(); // current index of character
        // try to extend the range [i, j]
        for (int j = 0, i = 0; j < n; j++) {
            if (map.containsKey(s.charAt(j))) {
                i = Math.max(map.get(s.charAt(j)), i);
            }
            ans = Math.max(ans, j - i + 1);
            map.put(s.charAt(j), j + 1);
        }
        return ans;
    }
}
}

代码如我们想象的那样。但是此处使用到了HashMap结构。
这就令我提出疑问,HashSet和HashMap有什么区别和联系呢。
请看接下来的一节。HashSet和HashMap的异同

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值