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;
        
    }
};
相关推荐
GDAL几秒前
uv 完整教程:下一代 Python 包管理工具
python·uv
写代码写到手抽筋7 小时前
5G上行DCI字段判定:端口 流数 PMI选择详解
java·算法·5g
曲幽7 小时前
FastAPI 身份验证总踩坑?这份 FastAPI Users “避坑指南”请收好
python·fastapi·web·jwt·oauth2·user·authentication
xieliyu.7 小时前
Java算法精讲:双指针(二)
java·开发语言·算法
苏宸啊7 小时前
IPC管道
linux·c++
装不满的克莱因瓶8 小时前
掌握 RNN 与 LSTM 模型结构
人工智能·python·rnn·深度学习·神经网络·ai·lstm
何以解忧,唯有..8 小时前
Python包管理工具pip:从入门到精通
开发语言·python·pip
BestOrNothing_20158 小时前
ROS2 话题通信实战:消息对象、Publisher 发布器与 Subscriber 订阅器保姆级教程
c++·ros2·subscriber·publisher·话题通信
wayz118 小时前
Momentum:PSL(心理线指标)技术指标详解
算法·金融·数据分析·量化交易·特征工程
金銀銅鐵8 小时前
用 Tkinter 实现简单的猜数字游戏
后端·python