【题目】

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.
【思路】
  滑动窗口Sliding Window 【代码】

public class Solution {
  public int lengthOfLongestSubstring(String s) {
  int len=s.length();
  int ans=0;int j=0;
  Map<Character,Integer> map=new HashMap<>();
  for(int i=0;i<len;i++){
    if(map.containsKey(s.charAt(i))){
      j=Math.max(j,map.get(s.charAt(i)));
    }
    ans=Math.max(ans,i-j+1);
    map.put(s.charAt(i),i+1);
  }
  return ans;
 }
}

 

[Leetcode 3] 最长不重复子串 Longest substring without repeating 滑动窗口的更多相关文章

  1. LeetCode.3-最长无重复字符子串(Longest Substring Without Repeating Characters)

    这是悦乐书的第341次更新,第365篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Medium级别的第2题Longest Substring Without Repeating Cha ...

  2. LeetCode 3: 无重复字符的最长子串 Longest Substring Without Repeating Characters

    题目: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. Given a string, find the length of the longest substring withou ...

  3. [Swift]LeetCode3. 无重复字符的最长子串 | Longest Substring Without Repeating Characters

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

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

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

  5. 【LeetCode从零单排】No 3 Longest Substring Without Repeating Characters

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

  6. 最长无重复字符的子串 · Longest Substring Without Repeating Characters

    [抄题]: 给定一个字符串,请找出其中无重复字符的最长子字符串. 例如,在"abcabcbb"中,其无重复字符的最长子字符串是"abc",其长度为 3. 对于, ...

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

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

  8. [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串

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

  9. [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串 C++实现java实现

    最长无重复字符的子串 Given a string, find the length of the longest substring without repeating characters. Ex ...

随机推荐

  1. Redis入门指南之二(安装及配置)

    本节主要内容 1. 前言2. redis安装3. 启动和停止Redis 1. 前言 安装Redis需要知道自己需要哪个版本,有针对性的安装,比如如果需要redis GEO这个地理集合的特性,那么red ...

  2. pythonl输出的预警消息中的json串的中文展示乱码(中文的unicode码)

    来源:ctrip接口名称:ReviewStandartAPI错误信息:[{'ReviewTime': u'\u63a5\u53e3\u8fd4\u56de\u7684\u70b9\u8bc4\u65f ...

  3. English Voice of <<City of stars>>

    City of stars 星光之城啊 Are you shining just for me? 你是否只愿为我闪耀 City of stars 星光之城啊 There's so much that ...

  4. CentOS上安装seafile

    一.安装python2.7.14(CentOS7上python默认版本是Python 2.7.5 ,不需要安装)1.安装依赖包[root@web01 ~]# yum -y install zlib z ...

  5. webpack+vue多入口环境搭建

    项目地址:https://pan.baidu.com/s/1c1Dflp2 使用前提:安装nodejs环境,webpack的配置官网的例子跟着跑一遍,会vue开发 研究webpack+vue研究了一个 ...

  6. 使用absolute布局

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. php根据时间显示刚刚,几分钟前,几小时前的实现代码

    发布时间距现在的时间 function tranTime($time) { $rtime = date("m-d H:i", $time); $htime = date(" ...

  8. 网络基础之 并发编程之进程,多路复用,multiprocess模块

    并发 1. 背景知识 2. 什么是进程 3. 进程调度 4. 并发与并行 5 同步\异步\阻塞\非阻塞(重点) 6.multiprocess模块 7.僵尸进程与孤儿进程 1.背景知识 一操作系统的作用 ...

  9. Python-Selenium中chromeDriver限制图片和Javascript加载

    我们有的时候使用Selenium会希望能够限制图片和Javascript执行,从而提高网页加载速度. options=webdriver.ChromeOptions() prefs={      'p ...

  10. win8.1安装密钥

    https://zhidao.baidu.com/question/374064869043943484.html