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 ...
随机推荐
- 题解【[USACO18FEB]New Barns 】
浅谈一下对于这题做完之后的感受(不看题解也是敲不出来啊qwq--) 题意翻译 Farmer John注意到他的奶牛们如果被关得太紧就容易吵架,所以他想开放一些新的牛棚来分散她们. 每当FJ建造一个新牛 ...
- DX12龙书 01 - 向量在几何学和数学中的表示以及运算定义
0x00 向量 向量 ( vector ) 是一种兼具大小 ( magnitude ) 和方向的量. 0x01 几何表示 几何方法中用一条有向线段来表示一个向量,其中,线段长度代表向量的模,箭头的指向 ...
- 安装memcache,步骤
1.先下载emcache.rar安装包<ps:随便安装在哪里文件夹,或者新建文件夹都是可以的> 2.解压安装包<ps:运行里面的程序> 3.然后根据自己的phpstudy的版本 ...
- EDI模拟实验
EDI模拟实验 [实验目的] ⑴.了解EDI报文的格式和特点. ⑵.掌握EDI报文生成和发送流程. [实验条件] ⑴.个人计算机一台,预装Windows XP操作系统和浏览器 ⑵.计算机通过局域网形式 ...
- nullptr解决了什么问题
从0到NULL 在C++的世界中字面值0用来表示空指针,所以0可以当作所有指针类型的字面值.为了让语义更明确引入了NULL宏定义: #undef NULL #ifdef __cplusplus #de ...
- selenium等待机制学习笔记
转载至: https://blog.csdn.net/huilan_same/article/details/52544521 1. 强制等待 第一种也是最简单粗暴的一种办法就是强制等待sleep(x ...
- LeCun自曝使用C语言23年之久,2年前才上手Python,还曾短暂尝试Lua!
程序员圈子的流行风潮,过几年就怀旧风走一波. 这不,最近Twitter上刮起了一阵编程语言使用历史的风潮. 连图灵奖得主.CNN之父-- Yann LeCun 也参与进来了. 他自曝使用C语言时间最长 ...
- Convert to Ones CodeForces(超水题)
题目大意:给你几个数,这些数里面只有0或1,你有两种操作:1.把一段区域内的所有数前后交换位置.2.把一段区域内所有数取反.(区域可大可小,可以是所有数也 ...
- Error:(4, 17) java: 程序包org.junit不存在
内容:Error:(4, 17) java: 程序包org.junit不存在 场景:运行测试类的时候,IED新建一个自己的项目,并且不用maven的情况下 解决方案:File -> Projec ...
- Helium文档6-WebUI自动化-S用于通过id\name\classname\xpth定位元素
前言 S方法可以灵活定位元素特别注意,比如to_left_of参数的使用是查找在某个元素左侧的元素,但是默认只会找第一个S方法可以灵活运用,特别是没有id,没有name,只有classname的情况, ...