【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 ...
随机推荐
- IntelliJ IDEA中怎么查看文件中所有方法?
可以使用快捷键ALT + 7打开左侧的Structure查看当前文件中的所有方法. 来自为知笔记(Wiz)
- 在Myeclipse中移除项目对Hibernate的支持
在Myeclipse中移除项目对Hibernate的支持 在使用Hibernate框架进行开发时可能会遇到配置错误或者需要删除Hibernate支持的情况.下面就说一下如何彻底移除项目的Hiberna ...
- ASP.NET地址栏form提交安全验证
以下类可以在web.config中直接配置,可以防范地址栏.表单提交的恶意数据. 安全模块作用: a.针对URL参数验证的功能,防止sql注入 b.针对form表单XSS漏洞的防护功能 c.针对上传文 ...
- Hierarchy Viewer
http://blog.csdn.net/ddna/article/details/5527072 Hierarchy Viewer是随AndroidSDK发布的工具,位置在tools文件夹下,名为h ...
- 【原】sql 查询结果合为一行
SELECT COUNT(*) AS AllCount,t.AssignedCount,(COUNT(*)-t.AssignedCount) AS UnassignedCountFROM 药品表jOI ...
- 黄聪:主机宝IIS版ISAPIRewrite伪静态软件操作演示
下载ISAPIRewrite伪静态破解文件 链接: http://pan.baidu.com/s/1dDEOLl3 密码: yx15 解压到主机宝ISAPIRewrite安装目录即可.如果提示有文件正 ...
- android按钮监听器的四种技术
android开发中经常会用到各种各样的监听器,android监听器的写法与java又有不同的地方; 1,activity中使用内部类实现接口 ,创建内部类实例 使用add方法 与java类似 创 ...
- 转-TabHost组件(二)(实现底部菜单导航)
http://www.cnblogs.com/lichenwei/p/3975095.html 上面文章<安卓开发复习笔记——TabHost组件(一)(实现底部菜单导航)>中提到了利用自定 ...
- Eclipse中构建Fluent风格到Formatter
The place to set this is on the "Line Wrapping" tab of the code formatting preferences pag ...
- 启动mongoDB 以及常用操作命令
nonsql 关系数据库 集合 表 文档 行 启动mongoDB之前首先手动创建存放MongoDB数据文件的目录,如e:\mongo_data 执行命令 mongod --dbpath=e: ...