题目链接:https://leetcode.com/problems/longest-substring-without-repeating-characters/

题目:Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.

解题思路:维护一个窗口,每次关注窗口中的字符串,维护一个HashSet,每扫描一个字符,如果HashSet中包含该字符,则移动左边窗口至重复字符的下一个字符,如果HashSet中没有改字符,则移动右边窗口。时间负复杂度为O(n),空间复杂度也为O(n)。具体代码如下:

public class Solution
{
public static void main(String[] args)
{
String str="abcbbc";
System.out.println(lengthOfLongestSubstring(str));
}
public static int lengthOfLongestSubstring(String s)
{
if(s==null && s.length()==0)
return 0;
HashSet<Character> set = new HashSet<Character>();
int max = 0; //最大不重复字符串长度
int left = 0; //不重复字符串起始位置
int right = 0; //不重复字符串结束为止
while(right<s.length())
{
//如果集合中包含当前所指的字符
if(set.contains(s.charAt(right)))
{
//获取当前不重复字符串的长度,如果大于max,则将max值替换
if(max<right-left)
{
max = right-left;
}
//将left移动到第一个重复的字符后面
while(s.charAt(left)!=s.charAt(right))
{
set.remove(s.charAt(left));
left++;
}
left++;
}
else
{
set.add(s.charAt(right));
}
right++;
}
max = Math.max(max,right-left);
return max;
}
}

【LeetCode OJ】Longest Substring Without Repeating Characters的更多相关文章

  1. 【Leetcode 3】Longest Substring Without Repeating Characters0

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

  2. LeetCode OJ:Longest Substring Without Repeating Characters(最长无重复字符子串)

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

  3. 【LeetCode】Longest Substring Without Repeating Characters 解题报告

    [题意] Given a string, find the length of the longest substring without repeating characters. For exam ...

  4. 【leetcode】Longest Substring Without Repeating Characters

    题目描述: Given a string, find the length of the longest substring without repeating characters. For exa ...

  5. 【leetcode】Longest Substring Without Repeating Characters (middle)

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

  6. 【LeetCode刷题系列 - 003题】Longest Substring Without Repeating Characters

    题目: Given a string, find the length of the longest substring without repeating characters. Example 1 ...

  7. 【LeetCode每天一题】Longest Substring Without Repeating Characters(最长无重复的字串)

    Given a string, find the length of the longest substring without repeating characters. Example 1:    ...

  8. 【Leetcode】【Medium】Longest Substring Without Repeating Characters

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

  9. 【leetcode刷题笔记】Longest Substring Without Repeating Characters

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

随机推荐

  1. 【WPF】WPF DataGrid List数据源 双向绑定通知机制之ObservableCollection使用以及MultiBinding 的应用

    以下代码实现了DataGrid的简单绑定List数据源 重点要提一下的是,绑定List数据源,但是不能直接用List.比如下面的代码,使用List<GridItem>只能实现数据修改的绑定 ...

  2. 【转】【WPF】关于依赖属性的ValidateValueCallback,PropertyChangedCallback和CoerceValueCallback的执行顺序

    三个回调对应依赖属性的验证过程,改变过程和强制转换过程. class Dobj : DependencyObject { //依赖属性包装 public int MyProperty { get { ...

  3. 求 pi 的近似值题型汇总

    (注:暂时先记录这些问题,后期会持续更新) 一.用格雷戈里公式计算π的近似值,精度要求:最后一项的绝对值小于0.00001 1,用while循环实现 int denominator,flag; dou ...

  4. JUnit规则

    在本节中,我们学习和了解JUnit中叫做规则的新功能,它允许非常灵活在测试类重新定义每个测试方法的行为.为了这个目的,@Rule注解被使用来标出测试类的公共字段.这些字段类型为MethodRule,这 ...

  5. Zookeeper系统设计的优点

    转自:Zookeeper系统设计的优点.http://webcache.googleusercontent.com/search?q=cache:s6fr40t_5ncJ:www.chaozh.com ...

  6. 图解DHCP的4步租约过程

    图解DHCP的4步租约过程   DHCP租约过程就是DHCP客户机动态获取IP地址的过程. DHCP租约过程分为4步: ①客户机请求IP(客户机发DHCPDISCOVER广播包): ②server响应 ...

  7. 让不支持MVC3的服务器/空间/虚拟主机 支持MVC3 (前提是必须支持 .net 4.0才可以)

    在有的时候,你用的VPS,服务器或者是虚拟空间可能是没有安装MVC3的,下面我们就自己来手动来让虚拟空间支持(前提是空间必须是.net 4.0的,因为MVC3是在4.0下运行) 我们先在web.con ...

  8. 植物 miRNA 研究

    相比动物miRNA 而言, 植物miRNA 的研究相对较少. 植物miRNA 相比动物miRNA , 有以下特点: 1) 植物miRNA 的长度为 21 nt 左右, 动物miRNA 长度在 22 ~ ...

  9. springmvc和encache集成

    虽然目前已经出了 ehcache3.x 了,但是,结合我搜索到的资料,我依旧只能先采用 ehcache2.x 来使用了 首先,在pom 中引入jar <dependency> <gr ...

  10. MYSQL之You can't specify target table for update in FROM clause解决办法

    mysql中You can't specify target table for update in FROM clause错误的意思是说,不能先select出同一表中的某些值,再update这个表( ...