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;
        
    }
};
相关推荐
伊玛目的门徒3 小时前
试用leetcode之典中典 二数之和问题
java·算法·leetcode
Jerry3 小时前
LeetCode 226. 翻转二叉树
算法
想做小南娘,发现自己是女生喵4 小时前
【无标题】
数据结构·算法
向日的葵0064 小时前
langchain的Tools教程(三)
python·langchain·tools
言乐66 小时前
Python实现可运行解密游戏游戏框架
python·游戏·小程序·游戏程序·关卡设计
Kx_Triumphs6 小时前
HDU4348 To the moon(主席树区间修改模板)
算法·题解
YUS云生6 小时前
Python学习笔记·第31天:FastAPI入门——路由、路径参数、查询参数与请求体
笔记·python·学习
旖-旎6 小时前
《LeetCode647 回文子串 || LeetCode 5 最长回文子串》
c++·算法·leetcode·动态规划·哈希算法
智写-AI6 小时前
真实有效的免费降英文AI工具服务商
人工智能·python