当前位置:网站首页>LeetCode第三题(Longest Substring Without Repeating Characters)三部曲之二:编码实现
LeetCode第三题(Longest Substring Without Repeating Characters)三部曲之二:编码实现
2022-08-03 08:15:00 【51CTO】
欢迎访问我的GitHub
这里分类和汇总了欣宸的全部原创(含配套源码): https://github.com/zq2599/blog_demos
- 本文是《LeetCode第三题(Longest Substring Without Repeating Characters)三部曲》的第二篇, 前一篇文章已经列出了完整的解题思路,今天来将此思路转化为具体的Java代码;
关键变量
- 编码之前先确定几个关键变量:
- 当前窗口中的元素都是不重复的,适合用一个HashSet来保存;
- max变量记录最长子串的长度;
- left表示窗口左侧相对整个字符串的位置,right表示窗口右侧相对整个字符串的位置,如下图:

代码实现
- 以下是代码,关键位置都有详细注释:
上述代码的关键是set.remove(s.charAt(left++)),配合着外面的while循环,"left++"表示将窗口向右移动一个元素,并且将窗口中最左侧的元素从set中删除;
上述代码在LeetCode上提交成功,不过运行时间超过40ms,成绩并不理想,接下来的文章我们一起来做优化提升速度;
欢迎关注51CTO博客:程序员欣宸
边栏推荐
猜你喜欢
随机推荐
WPF 学习笔记《WPF样式基础》
WordPress主题-B2美化通用子主题商业运营版
最佳高质量字体
五、《图解HTTP》报文首部和HTTP缓存
vs 2022无法安装 vc_runtimeMinmum_x86错误
如何在安装GBase 8c数据库的时候,报错显示“Host ips belong to different cluster?
工控机防勒索病毒浅析
国内IT市场还有发展吗?有哪些创新好用的IT运维工具可以推荐?
数据监控平台
rust 学习笔记
RViz报错: Error subscribing: Unable to load plugin for transport ‘compressed‘解决方法
ArcEngine(二)加载地图文档
Taro框架-微信小程序-调用微信支付
ArcEngine (5) use the ICommand interface to achieve zoom in and zoom out
AI mid-stage sequence labeling task: three data set construction process records
[Kaggle combat] Prediction of the number of survivors of the Titanic (from zero to submission to Kaggle to model saving and restoration)
集群
mysql5.7服务器The innodb_system data file 'ibdata1' must be writable导致无法启动服务器
【Kaggle实战】泰坦尼克号生存人数预测(从零到提交到Kaggle再到模型的保存与恢复)
Eject stubborn hard drives with diskpart's offline command









