zoukankan      html  css  js  c++  java
  • 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.

    class Solution {
    public:
        int lengthOfLongestSubstring(string s) {
            if ("" == s)return 0;
            map<char, size_t> char2uintMap;
            char ch = s[0];
            size_t ret_length = 0;
            size_t temp_length = 0;
    
            for (size_t i = 0;i < s.length();++i)
            {
                if (char2uintMap.find(s[i]) == char2uintMap.end())
                {
                    ++temp_length;
                    char2uintMap[s[i]] = 1;
                }else
                {
                    if (ret_length < temp_length)
                    {
                        ret_length = temp_length;
                    }
                    
                    for (int j = i - temp_length;j < i;++j)
                    {
                        if (s[j] == s[i])
                        {
                            break;
                        }
                        else
                        {
                            char2uintMap.erase(s[j]);
                            --temp_length;
                        }
                    }
                }
            }
            
    
            return ret_length<temp_length?temp_length:ret_length;
        }
    };
  • 相关阅读:
    python读取配置文件
    日志截取
    QJsonObject
    OpenStack
    生成器迭代器正则视频笔记
    使用Qt.createComponent 动态加载组件
    linux 远程执行命令
    Django 建立工程连接数据库
    arm基础1
    QSetting的用法
  • 原文地址:https://www.cnblogs.com/csudanli/p/6263275.html
Copyright © 2011-2022 走看看