【题目】

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. ip网关配置

    流量查看watch more /proc/net/devip子网查询https://www.sojson.com/convert/subnetmask.htmlhttp://ip.gchao.cn/ ...

  2. ssh服务及安全配置

    1.清空防火墙 关闭 setenforcesetenforce   2 getenforce 3 setenforce 0 4 iptables -F 5 systemctl stop firewal ...

  3. C# 获取当前服务器运行程序的根目录

    C# 获取当前服务器运行程序的根目录,获取当前运行程序物理路径 string tmpRootDir = AppDomain.CurrentDomain.BaseDirectory;//获得当前服务器程 ...

  4. String的intern()方法和java关键字、保留字

    String s1 = new StringBuilder("hel").append("lo").toString(); //hello System.out ...

  5. 有序广播和标准广播 --Android开发

    一.标准广播和有序广播也很容易理解的. 标准广播: (1)通过sendBroadcast()方法发送 (2)通过异步方式发送,广播接收者的执行顺序是不明确的 有序广播: (1)通过sendOrderB ...

  6. 20170822xlVBA ExportCellPhone

    Public Sub GetCellPhone() Dim CellPhone As String Dim Arr As Variant Dim Brr As Variant Dim n As Lon ...

  7. Confluence 6 教程:空间高手

    这个教程将会带你如何在 Confluence 中创建和自定义空间,同时也包括如何删除空间,如果需要的话.通过这个教程,你将成为使用空间的高手. 你需要具有创建空间(Create space)和创建个人 ...

  8. css 选择器三

    2.4.10 浮动 浮动是css里面布局最多的一个属性,也是很重要的一个属性. float:表示浮动的意思.它有四个值. none: 表示不浮动,默认 left: 表示左浮动 right:表示右浮动 ...

  9. python-flask基本应用模板

    1.模板继承 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...

  10. logstash快速入门

    转自 http://blog.csdn.net/wp500/article/details/41040213 原文地址:http://logstash.net/docs/1.4.2/tutorials ...