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 ...
随机推荐
- 学习使用turtlebot2——ROS上安装turtlebot2
安装环境: 安装Ubuntu 14.04版本和ROS Indigo 参考:http://wiki.ros.org/turtlebot/Tutorials 安装步骤 有两种安装方法,一种直接的安 ...
- Spring Mvc4 新特性(一)
前言 Spring Framework的Web层,由spring-web,spring-webmvc,spring-websocket和spring-webmvc-portlet模块组成. 很多人刚学 ...
- Spring 框架整合Struts2 框架和 Hibernate 框架
1. Spring 框架整合 Struts2 框架 // [第一种整合方式(不推荐)](http://www.cnblogs.com/linkworld/p/7718274.html) // 从 Se ...
- Spring 框架的AOP之注解的方式
1. 环境搭建 1.1 导入 jar 包 Spring 框架的基本开发包(6个); Spring 的传统AOP的开发包 spring-aop-4.3.10.RELEASE org.aopallianc ...
- IO 流之字节流和转换流
基本读取操作: InputStream(); OutputStream(); // 直接写入目的地中, 不需要 flush() 刷新 write(byte[] b); // 参数为 byte 数组 字 ...
- 我的Android进阶之旅------>解决Android Studio全局搜索搜不到结果的问题
1.问题描述 今天使用Android Studio时,想通过使用快捷键Ctrl+Shift+F来进行全局搜索指定字符串,如下图所示:想搜索字符串"码农偷懒了", 打开string. ...
- Drawable.Callback
一.介绍 public abstract void invalidateDrawable (Drawable who) Called when the drawable needs to be re ...
- Selenium 安装与卸载
安装: 在cmd中键入pip install selenium==3.6.0(等号后面的为版本号),并点击回车,当出现Successfully installed selenium-3.6.0即表示已 ...
- tomcat 配置文件 介绍
[root@mysql logs]# cd ../conf/ [root@mysql conf]# ll总用量 228drwxr-x---. 3 root root 4096 11月 15 2018 ...
- node.js---sails项目开发(6)--- 实现分页功能
只需要添加一个文件即可 api/blueprints/find.js 代码如下 /** * Module dependencies */ var util = require('util') ...