题目链接: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. Java虚拟机垃圾收集器与内存分配策略

    Java虚拟机垃圾收集器与内存分配策略 概述 那些内存须要回收,什么时候回收.怎样回收是GC须要完毕的3件事情. 程序计数器.虚拟机栈与本地方法栈这三个区域都是线程私有的,内存的分配与回收都具有确定性 ...

  2. 设置 sqlserver Profiler 只监控 EF的sql执行请求

    当我们用EF执行语句的时候,可以使用 sqlserver Profiler来监控到底执行了哪些sql语句,但是默认他是监控全局的,我们只想监控Ef的语句,这里如下设置 这样就只会监控 EF产生的 sq ...

  3. PyQt的signal 和 solit的补充

    from PyQt5.QtWidgets import (QWidget , QVBoxLayout , QHBoxLayout, QLineEdit, QPushButton) from PyQt5 ...

  4. TensorFlow:tf.reduce_mean(input_tensor, axis=None, keep_dims=False, name=None, reduction_indices=None)

    转载:https://www.cnblogs.com/yuzhuwei/p/6986171.html 1.概述 在深度学习里研究的物体的关系,都是比较复杂的.比如一个图片32X32大小的,它的像素信息 ...

  5. [转]十个 iOS 面试问题

    原文地址:http://onevcat.com/2013/04/ios-interview/ 不管对于招聘和应聘来说,面试都是很重要的一个环节,特别对于开发者来说,面试中的技术问题环节不仅是企业对应聘 ...

  6. lsof fuser

    使用fuser 或 lsof在一个挂载点中查找已打开的文件 fuser -mv /usr 查看有哪些进程在运行/usr中资源 sync fuser -km /media/usbdisk U盘无法卸载

  7. git远端删除被提交后又被加到.gitignore的文件

    远端删除文件而不影响本地文件 git rm [-r] --cached file_or_dir_name 利用.gitignore来自动删除所有匹配文件 我试过网上推荐的写法 git rm --cac ...

  8. …gen already exists but is not a source folder. Convert to a source folder or rename it [closed]

    Right click on the project and go to "Properties" Select "Java Build Path" on th ...

  9. Installshield Major upgrade

    Major upagrade: delete old version firstly, then install new version. need to change [product code] ...

  10. VC++调用MSFlexGrid的SetRow方法,出现异常“Invalid Row Value”

    MSFlexGrid是微软提供的网格表格控件,SetRow方法用于设置当前焦点所在行.  C++ Code  12345   void CMSFlexGrid::SetRow(long nNewVal ...