Longest Substring Without Repeating Characters,求没有重复字符的最长字串
问题描述:
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
算法分析:使用滑动窗口,设置两个指针,遍历字符串。
public class LongestSubstringWithoutRepeatingCharacters {
public int lengthOfLongestSubstring(String s)
{
int n = s.length();
Set<Character> set = new HashSet<>();
int ans = 0, i = 0, j = 0;//窗口左右指针
while(i < n && j < n)
{
if(!set.contains(s.charAt(j)))
{
set.add(s.charAt(j++));
ans = Math.max(ans, j - i);
}
else//如果有重复元素,则窗口左指针向右移动,直到移动到重复元素的下一位
{
set.remove(s.charAt(i++));
}
}
return ans;
}
}
Longest Substring Without Repeating Characters,求没有重复字符的最长字串的更多相关文章
- LeetCode: 3_Longest Substring Without Repeating Characters | 求没有重复字符的最长子串的长度 | Medium
题目: Given a . For . 解题思路: 这个题让找一个字符串中具有不重复单词的最长子串的长度,如:ababc,子串为abc,长度为3.有这么几个方法: 方法一: 依赖字符串本身的一些特有函 ...
- Leetcode3.Longest Substring Without Repeating Characters无重复字符的最长字串
给定一个字符串,找出不含有重复字符的最长子串的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 无重复字符的最长子串是 "abc",其长度为 ...
- LeetCode-3.无重复字符的最长字串
给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc&qu ...
- 【leetcode算法-中等】3. 无重复字符的最长字串
[题目描述] 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb"输出: 3 解释: 因为无重复字符的最长子串是 " ...
- LeetCode#3 - 无重复字符的最长字串(滑动窗口)
题目: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例: abcabcbb 输出的结果应该是3,最长的无重复的字串是'abc' 果然无论做什么都要静下心来啊!昨晚上卡了一个多小 ...
- 3. Longest Substring Without Repeating Characters寻找不重复的最大子串
首先弄清楚Substring和Subsequence,前者是子串,要求连续,后者是子序列,可以不连续 public int lengthOfLongestSubstring(String s) { / ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
- [leetcode]3. Longest Substring Without Repeating Characters无重复字母的最长子串
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
随机推荐
- ios开源东西
今天,我们将介绍20个在GitHub上非常受开发者欢迎的iOS开源项目,你准备好了吗? 1. AFNetworking 在众多iOS开源项目中,AFNetworking可以称得上是最受开发者欢迎的库项 ...
- Oracle database精装版11gR2入门详细连接教程
对于11g本身比较简单,适合学习者使用,对电脑要求相对较低. 自己一个人单机学习使用. 工具/原料 Oracle Database Express Edition 11g Release 2安装包 ...
- mfc 对话框程序 托盘实现
1 在头文件里面定义 消息 #define WM_SHOWTASK WM_USER+10 在主窗口类里面定义 一个变量 两个函数 a 变量 托盘结构体的变量 NOTIFYICONDATA m_nid; ...
- amazonservices api 抽象类 Class Abstraction
http://php.net/manual/zh/language.oop5.abstract.php MWSOrdersPHPClientLibrary-2013-09-01._V533357711 ...
- 码云平台, Git提交需要输入用户名/密码, 怎么办
这是因为, 检出代码的时候, 使用了http的方式: 那么, 改为ssh的地址就行了
- MySQL给字段唯一索引的三种方法
建表时添加 DROP TABLE IF EXISTS `student`; CREATE TABLE `student` ( `stu_id` ) NOT NULL AUTO_INCREMENT, ` ...
- PAGELATCH_x和PAGEIOLATCH_x介绍
Microsoft SQL Server企业级平台管理实践 第11章 Buffer Latch Timeout的解析 什么是PAGELATCH和PAGEIOLATCH 1.PAGELATCH_x和PA ...
- springboot接口 接收参数为实体对象跟MultipartFile对象报错。
要把文件和普通数据类型分开接口传输,不可以兼容多个类型参数, 建议是传文件一个接口,返回url路径,再和普通数据一起提交,就是两次 企业上的做法都是这样,先用文件服务器保存文件,返回文件路径 http ...
- BFC(Block Formatting Context)基础分析
W3C官方对于BFC的描述只有3小段,强烈建议想理解BFC的朋友先去看看,链接见文末. 常见的文档流分为:定位流.浮动流.普通流3种.BFC是普通流中的一种. 本文提出3个问题并给出使用BFC来解决这 ...
- Php DOMDocument 中的 formatOutput
Nicely formats output with indentation and extra space 是否处理 缩进和多余的空白符