题目描述

给定一个字符串,找出最长的不具有重复字符的子串的长度。例如,“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. ThreeJS系列1_CinematicCameraJS插件详解

    ThreeJS系列1_CinematicCameraJS插件详解 接着上篇 ThreeJS系列1_CinematicCameraJS插件介绍 看属性的来龙去脉 看方法作用 通过调整属性查看效果 总结 ...

  2. tu

    1 第五章 图 2 //结构定义 3 #define MaxVertexNum 100 //图中顶点数目的最大值 4 typedef struct ArcNode{ //边表节点 5 int adjv ...

  3. 唯品会Java开发手册》1.0.2版阅读

    <唯品会Java开发手册>1.0.2版阅读 1. 概述 <阿里巴巴Java开发手册>,是首个对外公布的企业级Java开发手册,对整个业界都有重要的意义. 我们结合唯品会的内部经 ...

  4. linux块设备驱动---相关结构体(转)

    上回最后面介绍了相关数据结构,下面再详细介绍 块设备对象结构 block_device 内核用结构block_device实例代表一个块设备对象,如:整个硬盘或特定分区.如果该结构代表一个分区,则其成 ...

  5. activiti 流程部署 保存流程图到数据库 保存二进制图片 存储失败

    activiti 流程部署 保存流程图到数据库  保存二进制图片 存储失败 具体错误如下 具体 junit测试 结果 :提示如下: 解决方法: 数据库版本不同 无法保存二进制文件到数据库表中!5.5. ...

  6. day20 Pyhton学习 面向对象-类与类之间的关系

    一.类与类之间的依赖关系 class Elphant: def __init__(self, name): self.name = name def open(self, ref): print(&q ...

  7. swoole一键协程

    swoole4.x后支持一键协程 加上后,开启一键协程化后,MySQL.Redis.Curl 等操作会变成异步 IO //此行代码后,文件操作,sleep,Mysqli,PDO,streams等都变成 ...

  8. go操作mysql数据库

    Golang GORM使用 gorm gorm是go语言中实现数据库访问的ORM(对象关系映射)库.使用这个库,我们可以利用面向对象的方法,更加方便的对数据库中的数据进行CRUD(增删改查). 基本使 ...

  9. 使用浏览器抓取QQ音乐接口(歌曲篇)

    前言 前面我们获取了歌曲的排行榜的数据,我们现在需要实现歌曲播放 前面我们写了一段函数来得到了回调的数据,现在我们需要使用这一段数据,来实现播放歌曲 完整代码 <!DOCTYPE html> ...

  10. ubuntu 搭建samba服务器&挂载(mount)代码到本地

    一.搭建samba服务器 1.下载: sudo apt-get install samba samba-common 2.创建共享文件夹MyShare: mkdir /home/user/MyShar ...