leetcode146 longest-substring-without-repeating-character
题目描述
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.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的更多相关文章
- length of the longest substring without repeating character
Given a string, find the length of the longest substring without repeating characters. 来源:力扣(LeetCod ...
- 3. Longest Substring Without Repeating Character[M] 最大不重复子串
题目 Given a string, find the length of the longest substring without repeating characters. Example 1: ...
- [刷题] 3 Longest Substring Without Repeating Character
要求 在一个字符串中寻找没有重复字母的最长子串 举例 输入:abcabcbb 输出:abc 细节 字符集?字母?数字+字母?ASCII? 大小写是否敏感? 思路 滑动窗口 如果当前窗口没有重复字母,j ...
- Leetcode 解题 Longest Substring without repeating charcater python
原题: Given a string, find the length of the longest substring without repeating character For example ...
- LeetCode[3] Longest Substring Without Repeating Characters
题目描述 Given a string, find the length of the longest substring without repeating characters. For exam ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- No.003:Longest Substring Without Repeating Characters
问题: Given a string, find the length of the longest substring without repeating characters.Example:Gi ...
- 【JAVA、C++】LeetCode 003 Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
- No.003 Longest Substring Without Repeating Characters
Longest Substring Without Repeating Characters Total Accepted: 167158 Total Submissions: 735821 Diff ...
- Java [leetcode 3] Longest Substring Without Repeating Characters
问题描述: Given a string, find the length of the longest substring without repeating characters. For exa ...
随机推荐
- vue 下载jquery 下载layui-layer 下载vue-router
1.下载jquery cmd:语句 npm install jquery 然后在main.js文件里面写 import $ from 'jquery' 2.下载layui-layer 在vue里面的l ...
- 【转】了解nodejs、javascript间的关系!bom&dom&ecmascript
地址:https://www.cnblogs.com/JetpropelledSnake/p/9450810.html bom&dom:https://www.cnblogs.com/wang ...
- jsoncpp笔记
Json Json是一种轻量级数据交换格式,可以表示数字,字符串,布尔值,null,数组,键值对: { "encoding" : "UTF-8", " ...
- 网络最大流Dinic
1.什么是网络最大流 形象的来说,网络最大流其实就是这样一个生活化的问题:现在有一个由许多水管组成的水流系统,每一根管道都有自己的最大通过水流限制(流量),超过这个限制水管会爆(你麻麻就会来找你喝茶q ...
- monolog封装
做一下基本关于Monolog的基本介绍: Monolog是基于PHP的日志类库. 介绍就到这,言归正传 安装 安装最新版本:(composer 还没安装的~:https://www.phpcompos ...
- go mod 使用bee工具
https://github.com/beego/bee/releases bee windows https://github.com/beego/bee/releases/download/v1 ...
- C++学习---单链表的构建及操作
#include <iostream> using namespace std; typedef struct LinkNode { int elem;//节点中的数据 struct Li ...
- xUtils简介和使用方法
xUtils简介 xUtils 包含了很多实用的android工具. xUtils 最初源于Afinal框架,进行了大量重构,使得xUtils支持大文件上传,更全面的http请求协议支持(10种谓词) ...
- 在centOS上安装oracle出现java.lang.NoClassDefFoundError问题及解决方法
问题一:CentOS6.5 静默安装Oracle 11G过程中提示:Exception in thread "main" java.lang.NoClassDefFoundErro ...
- 自动识别PC端、移动端,并跳转
PC端和移动端代码是分开的,各有一套代码的情况下: 在PC端的HTML文件head标签中间添加一段自动识别移动端的JavaScript代码: <script type="text/ja ...