LeetCode 热题 8/100打卡

Python:

python 复制代码
class Solution(object):
    def lengthOfLongestSubstring(self, s):
        """
        :type s: str
        :rtype: int
        """
        cnt=defaultdict(int)
        left =0
        ans=0
        for right,c in enumerate(s):
            cnt[c]+=1
            while cnt[c]>1:
                cnt[s[left]]-=1
                left+=1
            ans = max(ans,right-left+1)
        return ans
            
        

C++:

cpp 复制代码
class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        unordered_map<char,int>cnt;
        int left=0;
        int ans=0;
        int n=s.size();
        for(int right=0;right<n;right++){
            cnt[s[right]]++;
            while(cnt[s[right]]>1){
                cnt[s[left]]--;
                left++;
            }
            ans = max(ans,right-left+1);
        }
        return ans;
        
    }
};
相关推荐
写代码的小球1 小时前
求模运算符c
算法
tan180°4 小时前
MySQL表的操作(3)
linux·数据库·c++·vscode·后端·mysql
大千AI助手5 小时前
DTW模版匹配:弹性对齐的时间序列相似度度量算法
人工智能·算法·机器学习·数据挖掘·模版匹配·dtw模版匹配
彭祥.6 小时前
Jetson边缘计算主板:Ubuntu 环境配置 CUDA 与 cudNN 推理环境 + OpenCV 与 C++ 进行目标分类
c++·opencv·分类
烛阴6 小时前
简单入门Python装饰器
前端·python
lzb_kkk6 小时前
【C++】C++四种类型转换操作符详解
开发语言·c++·windows·1024程序员节
YuTaoShao6 小时前
【LeetCode 热题 100】48. 旋转图像——转置+水平翻转
java·算法·leetcode·职场和发展
好开心啊没烦恼6 小时前
Python 数据分析:numpy,说人话,说说数组维度。听故事学知识点怎么这么容易?
开发语言·人工智能·python·数据挖掘·数据分析·numpy
生态遥感监测笔记6 小时前
GEE利用已有土地利用数据选取样本点并进行分类
人工智能·算法·机器学习·分类·数据挖掘