Leecode-3 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.

解析

题目含义很简单,即求出没有字符重复的子字符串的长度。例如bbbbb很明显就是个由完全重复字符组成的字符串,所以它的答案长度为1。

解法1 : 遍历字符串构成无重复字符字符串

最简单的方法就是通过遍历字符串的每一个字符,循环的构造子字符串,遇到重复字符便停止,然后得出最长长度的那个字符串。

    public static int lengthOfLongestSubstring(String s) {
        int maxLength = 0;
        for(int index = 0 ; index<s.length() ; index++){
            StringBuffer temp = new StringBuffer(s.substring(index,index+1));
            for(int buffer = index+1 ; buffer < s.length() ; buffer++){
                // 取当前字符currStr
                String currStr = s.substring(buffer,buffer+1);

                // 如果temp里面包含了s[buffer],那么结束循环
                if(temp.indexOf(currStr)>=0){
                    if(temp.length()>maxLength)
                        maxLength = temp.length();
                    break;
                }else{
                    temp.append(s.charAt(buffer));
                }
            }
            // 如果该子串的长度大于当前最大长度,那么替换为最长长度
            if(temp.length()>maxLength){
                maxLength = temp.length();
            }
        }
        return maxLength;
    }

这个方法非常好理解,但是唯一的问题就是效率非常低,在外层有两层循环,在寻找字符操作时(temp.indexOf(currStr))也要循环一遍,所以该算法的复杂度为O(n^3)

解法2 滑动构造子串

    public static int lengthOfLongestSubstring(String s){
        // 非空校验
        if(s==null || s.isEmpty()){
            return 0;
        }
        int length = s.length();

        // 定义滑动标志位:indexOne,indexTwo s[indexOne]~s[indexTwo]之间的字符串组成一个不重复字符的子                
        // 串
        int indexOne = 0 , indexTwo = 0;
        int maxLength = 0;
        Set<Character> set = new HashSet<>();
        while(indexOne < length && indexTwo < length){

            // 如果set不包含新字符
            if(!set.contains(s.charAt(indexTwo))){

                // 那么indexTwo++ ,同时将新字符添加到set中
                set.add(s.charAt(indexTwo++));

                // 当前子串长度为 indexTwo - indexOne
                maxLength = Math.max(maxLength , indexTwo - indexOne);
            }else{
                set.remove(s.charAt(indexOne++));
            }
        }
        return maxLength;
    }

滑动构造子串的意思即通过两个索引indexOne,indexTwo动态构造子串,如果下一个字符没重复,那么indexTwo+1,如果下一个字符已重复,那么indexOne+1。
在最好的情况下,例如输入"abcde"的时候,下一个字符一直是新的字符,那么indexTwo可以一直+1,直到字符串被遍历完,这时候的效率为O(n)。
在最差的情况下,例如输入"bbbbb"的时候,下一个字符一直是重复字符,那么程序一直执行indexTwo+1后indexOne+1的循环,也即indexOne和indexTwo分别遍历了一遍了字符串,那么这时候的效率为O(2n)。
所以综合来看该算法的效率为O(n)。

转载于:https://www.cnblogs.com/benjamin4git/p/10112451.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值