【Leetcode】【Longest Substring Without Repeating Characters】【无重复字符的最长子串】【C++】

  • 题目:给定一字符串,求其无重复字符的最长子串长度。
  • 思路:for循环一次,时间复杂度为O(N)。字符的ascii值为32~126。start表示当前无重复字符子串的初始位置,初始值为0;可定义一个位置数组pos[128]表示for循环索引到当前位置时相应的字符对应的位置。若当前字符s[i](其ascii值为cur_pos),若pos[cur_pos]>=start,说明在start之后已有该字符s[i],则以start开始的子串第一次遇到重复字符,打住。判断当前长度是否大于max_len,若大于则更新max_len。然后更新start,以及pos[cur_pos]...。注意最后一个字符的处理。
  • 代码:

    class Solution {
    public:
        int lengthOfLongestSubstring(string s) {
            int len_s=s.size();
            const int M=128;
            int pos[M];
            for(int i=0;i<M;i++)
                pos[i]=-1;
    
            int start=0;
            int max_len=0;
    
            for(int i=0;i<len_s;i++)
            {
                int cur_pos=s[i];
                if(pos[cur_pos]>=start)
                {
                    if(max_len<(i-start))
                    {
                        max_len=i-start;
                    }
                    start=pos[cur_pos]+1;
                }
                pos[cur_pos]=i;
            }
            if((len_s-start)>max_len)
                max_len=len_s-start;
            return max_len;
        }
    };

原文地址:https://www.cnblogs.com/dreamer123/p/9157761.html

时间: 2024-10-20 16:08:14

【Leetcode】【Longest Substring Without Repeating Characters】【无重复字符的最长子串】【C++】的相关文章

3. Longest Substring Without Repeating Characters 无重复字符的最长子串

1. 原始题目 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3. 示例 2: 输入: "bbbbb" 输出: 1 解释: 因为无重复字符的最长子串是 "b",所以其长度为 1. 示例 3: 输入: "pwwkew" 输出: 3 解释: 因为无重复字符的最长子串是 &quo

leetcode——Longest Substring Without Repeating Characters 求链表中无重复字符的最大字串长度(AC)

mnesia在频繁操作数据的过程可能会报错:** WARNING ** Mnesia is overloaded: {dump_log, write_threshold},可以看出,mnesia应该是过载了.这个警告在mnesia dump操作会发生这个问题,表类型为disc_only_copies .disc_copies都可能会发生. 如何重现这个问题,例子的场景是多个进程同时在不断地mnesia:dirty_write/2 mnesia过载分析 1.抛出警告是在mnesia 增加dump

leetcode 3. 无重复字符的最长子串(Longest Substring Without Repeating Characters)

目录 题目描述: 示例 1: 示例 2: 示例 3: 解法: 题目描述: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3. 示例 2: 输入: "bbbbb" 输出: 1 解释: 因为无重复字符的最长子串是 "b",所以其长度为 1. 示例 3: 输入: "pwwkew"

LeetCode:Longest Substring Without Repeating Characters(最长不重复子串)

题目链接 Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest

[Swift]LeetCode3. 无重复字符的最长子串 | 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.

LeetCode——Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest subst

[LeetCode] Longest Substring Without Repeating Characters [15]

题目 Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest su

LeetCode: Longest Substring Without Repeating Characters 题解

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest subst

Leetcode——3. 无重复字符的最长子串

难度: 中等 题目 Given a string, find the length of the longest substring without repeating characters. 给定一个字符串,请你找出其中不含有重复字符的?最长子串?的长度. 示例?1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3. 示例 2: 输入: "bbbbb" 输出: 1 解释: 因为无重复字