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;
        }
    };
  • 相关阅读:
    Train Problem(栈的应用)
    Code obfuscatio (翻译!)
    Milking Cows
    Sorting a Three-Valued Sequence(三值排序)
    Asphalting Roads(翻译!)
    FatMouse' Trade
    Fibonacci Again
    Yogurt factory
    经济节约
    Lucky Conversion(找规律)
  • 原文地址:https://www.cnblogs.com/csudanli/p/6263275.html
Copyright © 2011-2022 走看看