zoukankan      html  css  js  c++  java
  • (一)贪心【C++刷题】

    无重复字符的最长子串

    Leetcode:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/

    1.问题描述

    给定一个字符串s,找出其中不含有重复字符的最长子串的长度。

    2.输入输出

    • Input:"abcabcbb"
    • Output:3

    3.算法分析

    滑动窗口:核心思想就是从每一个字符开始,找到不包含重复字符的最长子串。每次移动区间的起点或者终点,直到起点的值等于终点的值。

    • 时间复杂度:O(n)
    • 空间复杂度:O(1)

    4.编程实现

    #include <iostream>
    #include <string>
    using namespace std;
    
    class Solution {
    public:
        int lengthOfLongestSubstring(string s) {
            int start(0), end(0), length(0), result(0);
            int nSize = s.size();
            
            while (end < nSize) {  // 终点
                char tmpChar = s[end];
                for (int index = start; index < end; index++) {
                    if (tmpChar == s[index]) {
                        start = index + 1;
                        length = end - start;
                        break;
                    }
                }
                end++;
                length++;
                result = max(result, length);
            }
            
            return result;
        }
    };
    
    int main() {
        Solution sol;
        string s;
        cin >> s;
        
        cout << sol.lengthOfLongestSubstring(s) << endl;
        return 0;
    }
    
    本文为博主原创文章,未经博主允许禁止转载,需转载请注明出处,欢迎指正!
  • 相关阅读:
    tc: Linux HTTP Outgoing Traffic Shaping (Port 80 Traffic Shaping)(转)
    Linux TC的ifb原理以及ingress流控-转
    插件+组件+空间
    Q查询条件
    django中的分页标签
    QuerySet
    url
    view
    HttpReponse
    装饰器
  • 原文地址:https://www.cnblogs.com/caoer/p/15696978.html
Copyright © 2011-2022 走看看