zoukankan      html  css  js  c++  java
  • Leetcode03---Longest Substring Without Repeating Characters

    Description:

    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 substring is "b", with the length of 1.

    给定一个字符串,求字符串中最长不重复的字串长度。

    又是一道没有数据范围的题目,这里字符串长可以超过3w,所以暴力不可搞。最后用很巧妙的运用起点和重点位置的 顺序选择,和对字符先后出现位置的统计,来求得结果。

    class Solution {

    public:

        int lengthOfLongestSubstring(string s) {

            int maxLen = 0, start = -1;

            map<char, int>mapChar;

            for (int i = 0; s[i]; i ++) {

                if (mapChar.count(s[i])) {//这里学了一招,map的count方法可以统计对应类型出现的次数,不用再dt的初始化了

                    start = max(start, mapChar[s[i]]);

                }

                mapChar[s[i]] = i;

                maxLen = max(maxLen, i - start);

            }

            return maxLen;

        }

    };

  • 相关阅读:
    Nginx 相关配置文件修改
    LNMP平台构建实验 +bbs社区搭建
    CSGO项目
    创世战车项目
    IGXE搬砖项目
    11_samba服务器的搭建
    26_django内置static标签
    06_git添加远程仓库并向远程仓库中推送代码
    23_添加apps到项目的搜索路径
    23_django日志器的配置和其使用方法
  • 原文地址:https://www.cnblogs.com/luntai/p/4847594.html
Copyright © 2011-2022 走看看