• 题目描述

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

Example 1:

Input: "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.

Example 2:

Input: "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.

Example 3:

Input: "pwwkew"
Output: 3
Explanation: 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.
  • 思路分析

输出最大无重复字符的子字符串长度,很常规的数据结构题目。

最粗暴的BF算法就可以解决,当然时间复杂度会是O(n^2)这样的闹心样子。

小陌想到的方法是,建立数组存储字符串,从头开始捋,遇到重复的字符则从下一个元素开始下一轮的子字符串寻找,即每个子字符串从相应起点开始到下一个有重复字符位置。

小陌特别想分享下被圈粉的大神思路 :用HashSet维护当前字符串,左右两端一起搞。

例如我们从左边先开始,只要没有出现重复字符就继续左移;一旦出现重复字符,就换一条路,从右边开始。中间所忽略的那些,它们是有重复或者更短,值得“忽略”。而在左右开弓的情况下,至多对元素访问一遍,时间复杂度仅为O(n)。

大神的思路阐述:“基本思路是维护一个窗口,每次关注窗口中的字符串,在每次判断中,左窗口和右窗口选择其一向前移动。同样是维护一个HashSet, 正常情况下移动右窗口,如果没有出现重复则继续移动右窗口,如果发现重复字符,则说明当前窗口中的串已经不满足要求,继续移动有窗口不可能得到更好的结果,此时移动左窗口,直到不再有重复字符为止,中间跳过的这些串中不会有更好的结果,因为他们不是重复就是更短。”

  • 源码附录:

1.小陌的方法

class Solution {
public int lengthOfLongestSubstring(String s) {
if(s == null || s.length() == 0){
return 0;
} int[] count = new int[256];
int start = 0;
int end = 1;
int longest = 1; Arrays.fill(count,-1);
count[s.charAt(0)] = 0; while(end < s.length()){
if(count[s.charAt(end)] >= start){
start = count[s.charAt(end)] + 1;
} longest = Math.max(longest,end - start + 1);
count[s.charAt(end)] = end;
end ++;
} return longest;
}
}

2.大神的方法 (大神地址https://blog.csdn.net/linhuanmars/article/details/19949159

  class Solution {
public int lengthOfLongestSubstring(String s) {
if(s == null || s.length() == 0){
return 0;
} HashSet<Character> hs = new HashSet<Character>();
int start = 0;
int end = 0;
int longest = 0; while(end < s.length()){
if(hs.contains(s.charAt(end))){
longest = Math.max(longest,end - start);
while(s.charAt(start) != s.charAt(end)){
hs.remove(s.charAt(start));
start ++;
}
start ++;
}
else{
hs.add(s.charAt(end));
} end ++;
} longest = Math.max(longest,end - start);
return longest;
}
}

Longest Substring Without Repeating Characters---LeetCode进阶路③的更多相关文章

  1. Longest Substring Without Repeating Characters leetcode java

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

  2. Longest Substring Without Repeating Characters ---- LeetCode 003

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

  3. Longest Substring Without Repeating Characters -- LeetCode

    原题链接: http://oj.leetcode.com/problems/longest-substring-without-repeating-characters/ 这道题用的方法是在LeetC ...

  4. [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串

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

  5. leetcode: longest substring without repeating characters

    July 16, 2015 Problem statement: Longest Substring Without Repeating Characters Read the blog: http: ...

  6. LeetCode 3 Longest Substring Without Repeating Characters(最长不重复子序列)

    题目来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, f ...

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

    LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...

  8. LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters

    LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...

  9. [LeetCode][Python]Longest Substring Without Repeating Characters

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...

  10. 【一天一道LeetCode】 #3 Longest Substring Without Repeating Characters

    一天一道LeetCode (一)题目 Given a string, find the length of the longest substring without repeating charac ...

随机推荐

  1. 基于Openframeworks调取摄像头方式的定时抓拍保存图像方法小结

    这次是采用Openframeworks来调取摄像头画面并抓图保存. 开始 借向导自动生成代码,因为要调取摄像头设备,因此增添ofVideoGrabber对象声明,又因为保存需求,所以还需添加ofPix ...

  2. 大数据之路Week10_day01 (练习:通过设计rowkey来实现查询需求)

    1.准备数据 链接:https://pan.baidu.com/s/1fRECXp0oWM1xgxc0uoniAA 提取码:4k43 2.需求如下 (1)查询出10条某个人的最近出现的位置信息 (2) ...

  3. python基础-函数(lambda表达式、函数作参数、内置函数、推导式)和pip

    函数进阶 今日概要: 函数名就是一个变量(扩展) 匿名函数(lambda表达式) 重点内置函数--python内置函数 推导式(一行代码生成数据) 1. 函数名就是变量 def func(): pas ...

  4. [解决方案][docker] Http: server gave HTTP response to HTTPS client

    前言 用centos运行docker不支持http,需要在daemon.json 里面配置一下 目录 没有daemon.json 需要添加这个文件 在daemon.json 增添配置 重启即可 一.l ...

  5. C# Socket通信简单示例

    https://files.cnblogs.com/files/mojiejushi/SocketDemo.rar

  6. C++17 Filesystem 实用教程

    点击查看代码 C++17 标准带来了 std::filesystem库, 提供了强大的工具来处理文件路径, 目录以及其他与文件系统相关的操作. 这篇文章适合 C++ 初学者以及希望掌握 C++17 新 ...

  7. 130道基础OJ编程题之: 68~77

    130道基础OJ编程题之: 68~77 @ 目录 130道基础OJ编程题之: 68~77 68:BC72 平均身高 69:BC74 HTTP状态码 70:BC75 数字三角形 71:BC76 公务员面 ...

  8. Bringing machine 'default' up with 'virtualbox' provider... Your VM has become "inaccessible." Unfortunately, this is a critical error with VirtualBox that Vagrant can not cleanly recover from.

    启动虚拟机报错 vagrant up Bringing machine 'default' up with 'virtualbox' provider...Your VM has become &qu ...

  9. HTTP/1.1 优化

    避免发送 HTTP 请求 对于一些具有重复性的 HTTP 请求,比如每次请求得到的数据都一样的,我们可以把这对「请求-响应」的数据都缓存在本地,通过缓存技术减少请求次数. 客户端会把第一次请求以及响应 ...

  10. maven为什么发生依赖冲突?怎么解决依赖冲突?

    maven为什么发生依赖冲突?怎么解决依赖冲突? 我们在开发的时候,偶尔会遇到依赖冲突的时候,一般都是NoClassDefFoundError.ClassNotFoundException.NoSuc ...