leetcode - 3、Longest Substring Without Repeating Characters
题目链接:https://leetcode.com/problems/longest-substring-without-repeating-characters/description/
题目要求:求出最长的不重复子串
思路:
1、采用map,遍历字符串,将每个字符put进map
2、如果长度为1,直接输出1
3、end-begin+1(注意+1)
end为当前遍历到的位置,
begin为重复位置+1
4、在遍历字符串的时候,判断当前字符是否存在resultMap.containsKey(key),
如果不存在,将其put进map,并维护end-begin+1
如果存在,重置begin位置,计算长度
(begin的位置判断:如果当前begin位置index比重复位置大,需要begin==当前begin位置index,否则等于重复位置)
begin = begin > (resultMap.get(key)+1) ? begin : (resultMap.get(key)+1)
长度维护:
result = result > (end-begin+1) ? result : (end-begin+1);
public class Solution {
public int lengthOfLongestSubstring(String s) {
char str[] = s.toCharArray();
int len = str.length;
if(len == 1 ){
return 1;
}else {
// int sum = 0;
int result = 0;
int begin = 0;
int end = 0;
Map<String,Integer> resultMap = new HashMap<String, Integer>();
for(int i = 0; i<len; i++){
String key = str[i]+"";
end = i;
//未重复
if(!resultMap.containsKey(key)){
resultMap.put(key,i);
result = result > (end-begin+1) ? result : (end-begin+1);
}else { //遇到重复
// resultMap.clear();
begin = begin > (resultMap.get(key)+1) ? begin : (resultMap.get(key)+1);
result = result > (end-begin+1) ? result : (end-begin+1);
resultMap.put(key,i);
}
}
// System.out.println(result);
return result;
}
}
}
leetcode - 3、Longest Substring Without Repeating Characters的更多相关文章
- 【LeetCode】3 、Longest Substring Without Repeating Characters
题目等级:Medium 题目描述: Given a string, find the length of the longest substring without repeating chara ...
- 【一天一道LeetCode】 #3 Longest Substring Without Repeating Characters
一天一道LeetCode (一)题目 Given a string, find the length of the longest substring without repeating charac ...
- 【LeetCode OJ】Longest Substring Without Repeating Characters
题目链接:https://leetcode.com/problems/longest-substring-without-repeating-characters/ 题目:Given a string ...
- 【LeetCode】3. Longest Substring Without Repeating Characters 无重复字符的最长子串
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:无重复字符,最长子串,题解,leetcode, 力扣,py ...
- 【LeetCode】3.Longest Substring Without Repeating Characters 最长无重复子串
题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...
- 【LeetCode】3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- leetcode题解 3. Longest Substring Without Repeating Characters
题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...
- 【LeetCode】3. Longest Substring Without Repeating Characters (2 solutions)
Longest Substring Without Repeating Characters Given a string, find the length of the longest substr ...
- 《LeetBook》leetcode题解(3):Longest Substring Without Repeating Characters[M]——哈希判断重复
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
随机推荐
- json.loads(s) returns error message like this: ValueError: Invalid control character at: line 1 column 33 (char 33)
json.loads(s) returns error message like this: ValueError: Invalid control character at: line 1 colu ...
- 语义分割【semantic-segmentation】资料备忘
https://github.com/mrgloom/awesome-semantic-segmentation
- 实现Runnable接口和继承Thread类
如果欲创建的线程类已经有一个父类了,就不能再继承Thread类了,java不支持多继承. 实现Runnable接口: package multyThread; public class MyRuna ...
- 浅谈PHP面向对象编程(五、继承)
5.0 继承 5.1 继承的概念 在现实生活中,继承一般指的是子女继承父辈的财产.在程序中,继承描述的是事物之间的所属关系,通过继承可以使许多事物之间形成一种关系体系 . 例如猫和狗都属于动物,程序中 ...
- 新学一招,使用热门的Git
1.安装Git 1.本文所用版本为Git-1.8.0-preview20121022.exe , http://msysgit.googlecode.com/files/Git-1.8.0-prev ...
- News Master-DC and Marvel they are super heroes mother
News Master Good evening everyone,I’m Jason,I’m glad to be news master to share something, Tonight I ...
- Storm开发——环境配置部署
配置开发环境:http://storm.apache.org/releases/current/Setting-up-development-environment.html 开发环境定义: Stor ...
- makefile .phony targets
Phony Targets PHONY 目标并非实际的文件名:只是在显式请求时执行命令的名字.有两种理由需要使用PHONY 目标:避免和同名文件冲突,改善性能. 如果编写一个规则,并不产生目标文件,则 ...
- Java Servlet调用数据库复习
首先要导入jar包. 剩下的基本就是模版式的代码了: public class main { // JDBC 驱动名及数据库 URL static final String JDBC_DRIVER = ...
- Android MVP模式简单易懂的介绍方式 (三)
Android MVP模式简单易懂的介绍方式 (一) Android MVP模式简单易懂的介绍方式 (二) Android MVP模式简单易懂的介绍方式 (三) 讲完M和P,接下来就要讲V了.View ...