算法课程Leetcode作业第二周技术博客

算法课程Leetcode作业第二周技术博客

题目:3. Longest Substring Without Repeating Characters 难度:medium

题目描述

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.

分析

给定一个string,找出最长不重复的子String的长度,可以设定两个指针遍历String,每次遍历到下一次字符时检查是否之前出现过重复,我们可以用一个Set来存储已经出现过的字符,当新字符不在Set中时,记录当前长度并将该字符存储到Set中,当出现重复字符时,比较当前长度和历史最长长度,若当前长度大于历史最高,着将历史最高长度更改成当前长度,然后初始化Set和当前长度,将第二个指针移动一位从这里重新遍历,直到遍历完成。

代码

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        string::iterator it_1, it_2;
        it_1 = s.begin();
        it_2 = s.begin();
        set<char> set_check;
        int size_max = 0;
        int size_pre = 0;
        while(it_2 != s.end()) {
            char temp = *it_2;
            if (set_check.find(temp) != set_check.end()) {
                if (size_pre > size_max) {
                    size_max = size_pre;
                }
                size_pre = 0;
                set_check.clear();
                it_1++;
                it_2 = it_1;
            } else {
                set_check.insert(temp);
                size_pre++;
                it_2++;
            }
        }
        if (size_pre > size_max) {
            size_max = size_pre;
        }
        return size_max;
    }
};

image_1bq7jam5so8d14qa1pncc2r1q519.png-235kB

算法优化

第二个指针的移动其实可以不仅仅移动一位,而是移动到出现重复的字符的下一个字符开始

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        string::iterator it_1, it_2;
        it_1 = s.begin();
        it_2 = s.begin();
        set<char> set_check;
        set<char>::iterator set_it;
        int size_max = 0;
        int size_pre = 0;
        while (it_2 != s.end()) {
            char temp = *it_2;
            if (set_check.find(temp) == set_check.end()) {
                set_check.insert(temp);
                size_pre++;
                it_2++;
            } else {
                if (size_pre > size_max) {
                    size_max = size_pre;
                }
                while (*it_1 != temp) {
                    it_1++;
                }
                it_1++;
                it_2 = it_1;
                size_pre = 0;
                set_check.clear();
            }
        }
        if (size_pre > size_max) {
            size_max = size_pre;
        }
        return size_max;
    }
};

image_1bq7jgna21vn911vcuvm1ss61c0rm.png-181.1kB
可以发现算法的运行时间略微减少,不过还是可以看出在分布上,离平均的运行时间相差挺大

leetcode官方答案

image_1bq7jnu8v1ra2h7v186nfub5rq13.png-251.5kB
其实答案中未优化的部分和我的算法基本一致,就是用两个指针来遍历,但是官方的优化版本抛弃了使用Set来分辨一个字符的存在与否,而是定义了从字符到index的Map,这样就可以跳到我们要找的那个重复字符
用j来表示右边的那个指针位置,如果当前所指位置在Map中出现重复,着比较重复字符的index和i哪个大,将i更改为大的那个值
j - i + 1是当前长度,ans存储历史最长长度
算法将2n个步骤缩短为n个步骤,可以看出结果分析中运行时间减少的很明显
image_1bq7k186svsa1uv31doe1gaf8ls1g.png-189.6kB

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值