当前位置: 首页 > news >正文

面试经典150题——Day31

文章目录

    • 一、题目
    • 二、题解

一、题目

3. Longest Substring Without Repeating Characters

Given a string s, find the length of the longest
substring
without repeating characters.

Example 1:

Input: s = “abcabcbb”
Output: 3
Explanation: The answer is “abc”, with the length of 3.
Example 2:

Input: s = “bbbbb”
Output: 1
Explanation: The answer is “b”, with the length of 1.
Example 3:

Input: s = “pwwkew”
Output: 3
Explanation: The answer is “wke”, with the length of 3.
Notice that the answer must be a substring, “pwke” is a subsequence and not a substring.

Constraints:

0 <= s.length <= 5 * 104
s consists of English letters, digits, symbols and spaces.

题目来源: leetcode

二、题解

双指针+滑动窗口

class Solution {
public:int lengthOfLongestSubstring(string s) {int n = s.length();if(n == 0) return 0;int i = 0;unordered_map<char,int> map;map[s[i]] = 1;int res = 1;for(int j = 1;j < n;j++){while(map[s[j]] == 1 && i < j){map[s[i]] = 0;i++;}if(map[s[j]] != 1) map[s[j]] = 1;res = max(res,j - i + 1);}return res;}
};
http://www.lryc.cn/news/218549.html

相关文章:

  • chinese_llama_aplaca训练和代码分析
  • 大数据Doris(十七):关于 Partition 和 Bucket 的数量和数据量的建议
  • 进击的巨人 完结篇 后篇-中文下载
  • 力扣刷题-二叉树-二叉树的非递归遍历
  • react_15
  • 关于ROS的网络通讯方式TCP/UDP
  • Leetcode—421.数组中两个数的最大异或值【中等】明天写一下字典树做法!!!
  • 数智赋能!麒麟信安参展全球智慧城市大会
  • 基础课21——知识库管理
  • 网络运维Day01
  • 从零配置一台linux主机
  • 【蓝桥每日一题]-倍增(保姆级教程 篇1)
  • CNN(卷积神经网络)、RNN(循环神经网络)和GCN(图卷积神经网络)
  • 在markdown中怎么画表格
  • 每天五分钟计算机视觉:搭建手写字体识别的卷积神经网络
  • 【React】【react-globe.gl】3D Objects效果
  • 目标检测YOLO系列从入门到精通技术详解100篇-【目标检测】SLAM(补充篇)
  • Pytorch 缓解过拟合和网络退化
  • 【算法】昂贵的聘礼(dijkstra算法)
  • hackergame2023菜菜WP
  • ubuntu20.04.6使用FTP-及相关安全配置
  • C++中不允许复制的类
  • 使用Python 脚自动化操作服务器配置
  • DL Homework 6
  • 软考高项论文-绩效域
  • 设计模式之装饰模式--优雅的增强
  • 前端vue,后端springboot。如何防止未登录的用户直接浏览器输入地址访问
  • linux安装Chrome跑web自动化
  • linux环境下编译,安卓平台使用的luajit库
  • indexedDB笔记