【Leetcode】3.Longest Substring Without Repeating Characters 解题

【题目描述】:
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.

Subscribe to see which companies asked this question


【思路】:
使用一个vector<int>进行已扫描的字符对应’0’的asc码偏移存储。
使用两个int类型变量frontIndex与endIndex,分别记录substring收尾的位置。
使用一个int类型变量conflictIndex,记录冲突出现的字符在substring中的位置。
使用一个两个元素的数组indexArr进行frontIndex,endIndex的存储。

我的思路是就扫描,虽然我的代码放到Xcode(VS)中能运行,但是没达到leetcode的要求,显示超时了。需要去看看大神的思路,待补充:)


【我的代码】:
Status: Time Limit Exceeded

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        if (s == "") {
            return 0;
        }

        vector<int> *charVector = new vector<int>();
        int *indexArr = new int[2];
        indexArr[0] = 0;
        indexArr[1] = 0;
        int frontIndex = 0, endIndex = 0, conflictIndex = 0;

        const char *str = s.data();
        size_t len = s.size();

        for (endIndex = 0; endIndex < len; ++endIndex) {
            char desChar = str[endIndex];
            //判断该char存在性
            bool isExist = false;
            for (int j = 0; j < charVector->size(); ++j) {
                int desOffset = desChar - '0';
                if (desOffset == charVector->at(j)) {
                    isExist = true;
                    conflictIndex = frontIndex + j;
                    break;
                }
            }
            //若该char已存在
            if (isExist) {
                endIndex -= 1;
                //进行长度判断
                int lastLen = indexArr[1] - indexArr[0] + 1;
                int nowLen = endIndex - frontIndex + 1;
                if (nowLen > lastLen) {
                    indexArr[0] = frontIndex;
                    indexArr[1] = endIndex;
                }
                //准备新substring的检测
                charVector->clear();
                frontIndex = conflictIndex + 1;
                endIndex = frontIndex - 1;//for循环会+1
                continue;
            }
            //该char未存在
            charVector->push_back(desChar - '0');
            if (endIndex == len - 1) {
                //进行长度判断
                int lastLen = indexArr[1] - indexArr[0];
                int nowLen = endIndex - frontIndex;
                if (nowLen > lastLen) {
                    indexArr[0] = frontIndex;
                    indexArr[1] = endIndex;
                }
            }
        }
        return indexArr[1] - indexArr[0] + 1;
    }
};

【参考答案1代码】:
Time complexity : O(n^3).
Space complexity : O(min(n, m))O(min(n,m)). We need O(k)O(k) space for checking a substring has no duplicate characters, where kk is the size of the Set. The size of the Set is upper bounded by the size of the string nn and the size of the charset/alphabet mm.

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

private:
    bool allUnique(String s, int start, int end) {
        set<char> charSet;
        for (int i = start; i < end; ++i) {
            char ch = s.at(start);
            if (charSet.find(ch) != charSet.end()) return false;
            charSet.insert(ch);
        }
        return true;
    }
}

【参考答案2代码】:
Time complexity : O(2n) = O(n)O(2n)=O(n). In the worst case each character will be visited twice by i and j.
Space complexity : O(min(m, n))O(min(m,n)). Same as the previous approach. We need O(k)O(k) space for the sliding window, where kk is the size of the Set. The size of the Set is upper bounded by the size of the string nn and the size of the charset/alphabet mm.

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        set<char> charSet;
        int n = s.size();
        int i = 0, j = 0, ans = 0;
        while (i < n && j < n) {
            if (charSet.find(s.at(j)) == charSet.end()) {
                charSet.insert(s.at(j++));
                ans = max(ans, j - i);
            } else {
                charSet.erase(s.at(i++));
            }
        }
        return ans;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值