zoukankan      html  css  js  c++  java
  • [Leetcode]第三题:无重复字符最长子串

    Information

    author-info wechatOfficialAccount

    Problem Description

    给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。

    示例 1:

    输入: "abcabcbb"
    输出: 3 
    解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。
    

    示例 2:

    输入: "bbbbb"
    输出: 1
    解释: 因为无重复字符的最长子串是 "b",所以其长度为 1。
    

    示例 3:

    输入: "pwwkew"
    输出: 3
    解释: 因为无重复字符的最长子串是 "wke",所以其长度为 3。
         请注意,你的答案必须是 子串 的长度,"pwke" 是一个子序列,不是子串。
    

    Problem Solving

    记录重复出现的位置,每当重复则删除数组重复位置之前的部分。

    Python solution

    class Solution:
        def lengthOfLongestSubstring(self, s):
            list_=[]
            max_=0
            for c in s:
                if c in list_:del list_[0:list_.index(c)+1]
                list_.append(c)
                max_=len(list_) if len(list_)>max_ else max_
            return max_
    

    C++ solution

    • 使用两个指针记录当前最大不重复序列,遍历序列不断修改指针。同时,用max_记录每次循环的最大不重复值。

    • string 的子串函数

      string::size_type substr(int pos,int len);
      
    • string中子串的位置

      s.find(substring);    
      
    • 使用s.npos判断是否找到

      s.find(substring)==s.npos
      
      class Solution
      {
        public:
          int lengthOfLongestSubstring(string s)
          {
              if (s.size() <= 1)
                  return s.size();
      
              int max_ = 1;
              int i = 0, j = 1;
              for (j = 1; j < s.size(); j++)
              {
                  string sub = s.substr(i, j-i);
                  int pos = sub.find(s[j]);
                  if (pos != sub.npos)
                      i += pos + 1;
                  else if (1 + j - i > max_)
                      max_ = 1 + j - i;
              }
              return max_;
          }
      };
      
    • 使用哈希映射。使用长度为128的数组映射字母及其位置关系

    • 使用ios::sync_with_stdio(false)来解除std::cinstd::coutstdio中输入输出的同步;使用cin.tie(nullptr) 来解除输入输出的绑定;以上两个操作节约运行时间。

      static int x = [](){ios::sync_with_stdio(false); cin.tie(nullptr); return 0; }();
      class Solution {
      public:
          int lengthOfLongestSubstring(string s) {
              vector <int>hax(128,-1);
              int res=0;
              int left=-1;
              for(int i=0;i<s.size();++i)
              {
                  left=max(left,hax[s[i]]);
                  hax[s[i]]=i;
                  res=max(i-left,res);
              }
              return res;
          }
      };
      
  • 相关阅读:
    vue-hbuilder打包-调取摄像头或上传图片
    vue使用websoket(非封装)
    vue项目hbuilder打包-微信登录调取手机微信登录权限
    登录信息localStorage存储
    vue的三种组件传值方式
    vue-axios设置公共的请求ip
    hive基本配置
    The server time zone value 'EDT' is unrecognized or represents more than one time zone.
    hive启动时 java.net.ConnectException:拒绝连接
    FAILED: HiveException java.lang.RuntimeException: Unable to instantiate org.apache.hadoop.hive.ql.me
  • 原文地址:https://www.cnblogs.com/yczha/p/13160202.html
Copyright © 2011-2022 走看看