zoukankan      html  css  js  c++  java
  • LC 3. Longest Substring Without Repeating Characters

    题目描述

    Given a string, find the length of the longest substring without repeating characters.

    Example 1:

    Input: "abcabcbb"
    Output: 3 
    Explanation: The answer is "abc", with the length of 3. 
    

    Example 2:

    Input: "bbbbb"
    Output: 1
    Explanation: The answer is "b", with the length of 1.
    

    Example 3:

    Input: "pwwkew"
    Output: 3
    Explanation: 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.

    参考答案

     1 class Solution {
     2 public:
     3     int lengthOfLongestSubstring(string s) {
     4 
     5         unordered_map<char,int> map;
     6         int res = 0;
     7         int start = -1; // 存储开始的位置,如果完全没有重复的字符,那么开始位置永远是-1
     8         
     9         for(int i = 0;i<s.length();i++){
    10             if(map.find(s[i]) != map.end()){ 
    11                 start = max(start,map[s[i]]); 
    12                 // 如果通过find 找到了一个重复的
    13                 // 那么就把start给替换成 上一个重复字符的index
    14             }
    15             map[s[i]] = i; // map[char] = index;
    16             res = max(res,i-start); // 获得最大值
    17         }
    18         return res;
    19     }
    20 };    

    Given a string, find the length of the longest substring without repeating characters.

    Example 1:

    Input: "abcabcbb"
    Output: 3 
    Explanation: The answer is "abc", with the length of 3. 
    

    Example 2:

    Input: "bbbbb"
    Output: 1
    Explanation: The answer is "b", with the length of 1.
    

    Example 3:

    Input: "pwwkew"
    Output: 3
    Explanation: 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.
  • 相关阅读:
    openssl签署和自签署证书的多种实现方式
    OpenSSL主配置文件openssl.cnf
    openssl x509(签署和自签署)
    openssl rsautl和openssl pkeyutl(文件的非对称加密)
    openssl dgst(生成和验证数字签名)
    openssl passwd
    openssl speed和openssl rand
    openssl rsa/pkey
    openssl genrsa
    OpenSSL命令系列
  • 原文地址:https://www.cnblogs.com/kykai/p/11620126.html
Copyright © 2011-2022 走看看