【LeetCode】3.Longest Substring Without Repeating Characters 最长无重复子串
题目:
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb"
, the answer is "abc"
, which the length is 3.
Given "bbbbb"
, the answer is "b"
, with the length of 1.
Given "pwwkew"
, the answer is "wke"
, with the length of 3. Note that the answer must be a substring, "pwke"
is a subsequence and not a substring.
思路:
初始字符上一次出现的位置-1,初始子串开始位置-1。对字符串中每个字符元素遍历,判断更新子串的开始位置及最大长度,循环结束后 返回最大长度max.
public class Solution {
public int lengthOfLongestSubstring(String s) {
int[] local=new int[128];
int index=-1;
int max=0; for(int a=0;a<local.length;a++){
local[a]=-1;
} for(int i=0;i<s.length();i++){ if(local[(int)s.charAt(i)]>index){
index=local[(int)s.charAt(i)];
} if(i-index>max){
max=i-index;
} local[(int)s.charAt(i)]=i;
} return max;
}
}
【LeetCode】3.Longest Substring Without Repeating Characters 最长无重复子串的更多相关文章
- [LeetCode] 3.Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
- leetcode 3 Longest Substring Without Repeating Characters最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- [LeetCode] Longest Substring Without Repeating Characters最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串 C++实现java实现
最长无重复字符的子串 Given a string, find the length of the longest substring without repeating characters. Ex ...
- 【LeetCode每天一题】Longest Substring Without Repeating Characters(最长无重复的字串)
Given a string, find the length of the longest substring without repeating characters. Example 1: ...
- LeetCode Longest Substring Without Repeating Characters 最长不重复子串
题意:给一字符串,求一个子串的长度,该子串满足所有字符都不重复.字符可能包含标点之类的,不仅仅是字母.按ASCII码算,就有2^8=128个. 思路:从左到右扫每个字符,判断该字符距离上一次出现的距离 ...
- 003 Longest Substring Without Repeating Characters 最长不重复子串
Given a string, find the length of the longest substring without repeating characters.Examples:Given ...
随机推荐
- CK方程
上文中,“到时间n为止进入任意一个特定的状态集合”应理解为“在时间n及之前进入的都算”. 只要进入了该状态集合,之后是否离开已经不重要了.这个可类比于“先赢若干局”的赌徒问题:即使在赢得若干局后继续赌 ...
- RabbitMQ和Kafka
转自通九大神的博客 起因 最近公司RabbitMQ的集群出了点问题,然后有些亲就说RabbitMQ慢且不好用,是一个瓶颈,不如换成Kafka.而我本人,使用RabbitMQ有一点久了,认为这个事情应当 ...
- 【转】SQL SERVER 开窗函数简介
在SQL SERVER 2005/2008支持两种排名开窗函数和聚集开窗函数. 以SQL SERVER中分面页为例,按时间顺序列出定单号. WITH OrderInfo AS ( SELECT ROW ...
- 黄聪:WordPress图片插件:Auto Highslide修改版(转)
一直以来很多人都很喜欢我博客使用的图片插件,因为我用的跟原版是有些不同的,效果比原版的要好,他有白色遮罩层,可以直观的知道上下翻图片和幻灯片放映模式.很多人使用原版之后发现我用的更加帅一些,于是很多人 ...
- xubuntu手记
一. xfce下intelij快捷键冲突 window manager 快捷键 keyboard快捷键
- mvc 权限管理 demo
http://blog.csdn.net/zht666/article/details/8529646 new http://www.cnblogs.com/fengxing/archive/2012 ...
- eclipse 每次切换工作空间都要重新配置
首先,导出T1中的配置打开T1,选择file --> Export --> 在弹出框中选择General 下的preference --> next --> 在export p ...
- SpringAOP所支持的AspectJ切点指示器
在spring中尝试使用AspectJ其他指示器时,将会抛出IllegalArgumentException异常. 当我们查看上面展示的这些spring支持的指示器时,注意只有execution指示器 ...
- laravel查询构造器中别名的问题
Laravel框架对数据库的封装是比较完善的,用起来也比较方便.但之前有一个问题一直困扰着我,就是利用laravel作查询时.如果想给表名或是字段名起别名是比较麻烦的事.但翻阅它的文档不难发现,它提供 ...
- python输出重定向
0表示标准输入1表示标准输出2表示标准错误输出> 默认为标准输出重定向,与 1> 相同2>&1 意思是把 标准错误输出 重定向到 标准输出.&>file 意思是 ...