题目描述

给定一个字符串,找出最长的不具有重复字符的子串的长度。例如,“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. 【题解】[ZJOI2009]假期的宿舍

    \(\color{red}{Link}\) \(\text{Solution:}\) 把人和床看成点,问题转化为二分图. 于是,对于每一个在校生,我们建立出他的床点:然后对于每一个在校生,他们自己可以 ...

  2. ASP。NET Web表单模型,部分呈现和事件

    下载EventExample.zip - 41.33 KB 下载EventandAjaxExample.zip - 41.94 KB 介绍 通过参考ASP获得Web应用程序环境及其约束的概述.NET ...

  3. leaflet中如何优雅的解决百度、高德地图的偏移问题

    话不多说,先上效果图 以前在做项目时,经常会听到客户说,你们这个地图是哪来的,太丑了,能不能换成百度地图--高德也行-- 大家生活中,基本上都已经习惯了使用百度地图和高德地图,而在做项目时,用这两个地 ...

  4. 多测师讲解自动化--rf断言(下)--_高级讲师肖sir

    1.Page Should Contain 1.1存在页面上的内容 1.2 不存在页面上的内容, 运行抛异常 1.3 Page Should Not Contain 1.4 1.5 输入页面元素,判断 ...

  5. go http爬虫

    1 package main import ( "fmt" "io/ioutil" "net/http" ) func main() { r ...

  6. web功能测试

    web功能测试基础: https://www.cnblogs.com/wz123/p/9680484.html

  7. 两分钟搞定VS下第三方库的配置(以GNU Regex Library库为例)

    写C的朋友大概知道导入一个库的痛苦,特别是在宇宙第一IDE--VS下更是无从下手,生怕一不小心就把VS搞崩了,而VS的卸载过程又是一个十分头疼的过程.所以,这里特此开了一篇如何在VS下配置第三方库的博 ...

  8. PHP实现Bitmap的探索 - GMP扩展使用

    原文地址:https://blog.fanscore.cn/p/22/ 一.背景 公司当前有一个用户群的系统,核心功能是根据不同的条件组去不同的业务线中get符合条件的uid列表,然后存到redis中 ...

  9. [bug]录

    后端请求地址找不到,Tomcat服务器无响应 报404错误 查看资料和视频,用了2天时间(实际不到2天),还没有解决,我在书上找了描述,也问了别人,路径改成图上所说,还是未解决,找资源找到不广泛,没找 ...

  10. Vue基础(1)

    Vue简介 1.JavaScript框架 2.简化Dom操作 3.响应式数据驱动 Vue基础 通过下面代码引用vue: <script src="https://cdn.jsdeliv ...