题目描述

给定一个字符串,找出最长的不具有重复字符的子串的长度。例如,“abcabcbb”不具有重复字符的最长子串是“abc”,长度为3。对于“bbbbb”,最长的不具有重复字符的子串是“b”,长度为1。

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.
import java.util.*;
import java.util.HashMap;
/*
滑动窗口,比方说,abcabccc  当你右边扫描到abca的时候,你得把第一个a删掉得到bca,
然后,“窗口”继续向右滑动,每当加到一个新的char的时候,左边检查有无重复的char,然后如果没有重复的就正常添加
有重复的话就左边扔掉一部分,从最左到重复char这段扔掉,在这个过程中记录最大窗口长度

*/

public class Solution {
    /**
     *
     * @param s string字符串
     * @return int整型
     */
    public int lengthOfLongestSubstring (String s) {
        // write code here
        if (s==null || s.length()==0) return 0;
        HashMap <Character,Integer> map=new HashMap<Character ,Integer>();
        int leftBound=0;
        int max=0;
        for (int i=0;i<s.length();i++){
            char c=s.charAt(i);
            leftBound=Math.max(leftBound,(map.containsKey(c))?map.get(c)+1:0);
            max=Math.max(max,i-leftBound+1);
            map.put(c,i);
            
        }
        return max;
        
    }
}

leetcode146 longest-substring-without-repeating-character的更多相关文章

  1. length of the longest substring without repeating character

    Given a string, find the length of the longest substring without repeating characters. 来源:力扣(LeetCod ...

  2. 3. Longest Substring Without Repeating Character[M] 最大不重复子串

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

  3. [刷题] 3 Longest Substring Without Repeating Character

    要求 在一个字符串中寻找没有重复字母的最长子串 举例 输入:abcabcbb 输出:abc 细节 字符集?字母?数字+字母?ASCII? 大小写是否敏感? 思路 滑动窗口 如果当前窗口没有重复字母,j ...

  4. Leetcode 解题 Longest Substring without repeating charcater python

    原题: Given a string, find the length of the longest substring without repeating character For example ...

  5. LeetCode[3] Longest Substring Without Repeating Characters

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

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

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

  7. No.003:Longest Substring Without Repeating Characters

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

  8. 【JAVA、C++】LeetCode 003 Longest Substring Without Repeating Characters

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

  9. No.003 Longest Substring Without Repeating Characters

    Longest Substring Without Repeating Characters Total Accepted: 167158 Total Submissions: 735821 Diff ...

  10. Java [leetcode 3] Longest Substring Without Repeating Characters

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

随机推荐

  1. mac常用指令

    mkdir demo //创建一个文件夹 touch index.html // 创建一个html文件 rm rouch index.html //删除找个index.html文件 rmdir dem ...

  2. Spring Boot+Spring Security+JWT 实现 RESTful Api 认证(二)

    Spring Boot+Spring Security+JWT 实现 RESTful Api 认证(二) 摘要 上一篇https://javaymw.com/post/59我们已经实现了基本的登录和t ...

  3. Linux 下 svn 场景实例及常用命令详解

    一.SVN使用场景实例 问题: 在使用svn做为版本控制系统的软件开发中,经常会有这样的需求:在工作复本目录树的不同目录中增加了很多文件,但未纳入版本控制系统,这时如果使用svn add命令一个一个的 ...

  4. 【git】关联本地仓库与远程仓库

    1.在远程建立一个空项目[项目名称]2.git init3.git remote add origin [git 地址]4.git pull origin master5.git push origi ...

  5. Pycharm开发环境配置与调试

    在Windows宿主机上搭建Ubuntu虚拟机的Pycharm开发环境,Ubuntu开启Samba服务,使用网络映射将Ununtu下Python项目工程路径映射到Windows下 创建Pycharm工 ...

  6. Linux中断驱动程序

    1.中断概念 中断时一种电信号,由硬件设备产生,然后再由中断控制器向处理器发送相应的信号.处理器一经检测到该信号,便中断自己当前正在处理的工作,转而去处理中断.此后,处理器会通知操作系统已经产生中断. ...

  7. CV学习日志:CV开发常用库及其头文件

    CV开发过程中,通常会涉及以下库:(1)语言/视觉:C.CPP.QT.OpenCV(2)通信/模拟:ROS2.Gazebo.Webots(3)日志/数学:Eigen3.Gflags.Glog.Cere ...

  8. python selenium 自动登陆

    #-*- coding:utf8 -*- # 导入selenium2中的webdriver库 from time import sleep from selenium import webdriver ...

  9. xUtils简介和使用方法

    xUtils简介 xUtils 包含了很多实用的android工具. xUtils 最初源于Afinal框架,进行了大量重构,使得xUtils支持大文件上传,更全面的http请求协议支持(10种谓词) ...

  10. django—Form组件校验方法(is_valid)执行流程

    1.从is_valid方法入手 def is_valid(self): """Return True if the form has no errors, or Fals ...