leetcode — longest-substring-without-repeating-characters
import java.util.HashSet;
import java.util.Set;
/**
* Source : https://oj.leetcode.com/problems/longest-substring-without-repeating-characters/
*
* Created by lverpeng on 2017/6/26.
*
* Given a string, find the length of the longest substring without 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.
*
*/
public class LongestSubstring {
/**
* 两个指针:一个指向当前字符,一个指向当前子串开始的位置
* 将指向当前子串的指针依次向后移动,判断当前哈希表(用字符作为key,value不重要,可以直接用set)中是否存在当前字符
* 如果已经存在,说明找到一个子串
* 将指向当前子串的指针向后移动一个位置,作为新的子串起始位置
* 计算刚刚结束子串的长度,和之前长度作比较,选出较大的一个
* 如果不存在,更新当前子串的长度,加1
* 将当前字符加入hash表,将当前指针向后移动,重复上面的操作
*
*
*
* @param str
* @return
*/
public int searchLongestSubstring (String str) {
int lastIndext = 0;
int maxLen = 0;
Set<Character> charSet = new HashSet<Character>();
for (int i = 0; i < str.length(); i++) {
if (charSet.contains(str.charAt(i))) {
int newLen = i - lastIndext;
maxLen = Math.max(newLen, maxLen);
lastIndext ++;
} else {
maxLen ++;
}
charSet.add(str.charAt(i));
}
return maxLen;
}
public static void main(String[] args) {
LongestSubstring longestSubstring = new LongestSubstring();
System.out.println(longestSubstring.searchLongestSubstring("abcabcbb"));
}
}
leetcode — longest-substring-without-repeating-characters的更多相关文章
- [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
July 16, 2015 Problem statement: Longest Substring Without Repeating Characters Read the blog: http: ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
- C++ leetcode Longest Substring Without Repeating Characters
要开学了,不开森.键盘声音有点大,担心会吵到舍友.今年要当个可爱的技术宅呀~ 题目:Given a string, find the length of the longest substring w ...
- [LeetCode]Longest Substring Without Repeating Characters题解
Longest Substring Without Repeating Characters: Given a string, find the length of the longest subst ...
- [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. For exam ...
- 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 (C++)
题目: Given a string, find the length of the longest substring without repeating characters. For examp ...
- [LeetCode] Longest Substring Without Repeating Characters (LinkedHashSet的妙用)
Given a string, find the length of the longest substring without repeating characters. For example, ...
随机推荐
- 20172325 2018-2019-2 《Java程序设计》第九周学习总结
20172325 2018-2019-2 <Java程序设计>第九周学习总结 教材学习内容总结 图的定义 图是由顶点集(VertexSet)和边集(EdgeSet)组成,针对图G,顶点集和 ...
- solr7.7.0搜索引擎使用(三)(添加文件索引)
众所周知,solr与es的最大区别是,solr可以对pdf,txt,doc等文件生成索引 那我们如何添加文件索引呢? 步骤1.添加core,取名暂且为 coreFile 在bin下执行命令 ./sol ...
- Linux 6上使用UDEV绑定共享存储
1.硬盘的查看方式 [root@cl6-11gr2-rac1 ~]# ls -ltr /dev/sd* brw-rw----. 1 root disk 8, 48 8月 16 13:34 /dev/s ...
- C# Winform 登录中的忘记密码及自动登录
本地保存登录账号实现忘记密码及自动登录 #region 删除本地自动登录及记住密码信息 /// <summary> /// 删除本地自动登录及记住密码信息 /// </summary ...
- GBDT(MART) 迭代决策树详解
在网上看到一篇对从代码层面理解gbdt比较好的文章,转载记录一下: GBDT(Gradient Boosting Decision Tree) 又叫 MART(Multiple Additive Re ...
- Linux 第五天
网络命令 1)write 给在线用户发信息(需按Crtl+D保存结束,w命令可看在线用户) 语法:write 用户名 2)wall 发广播信息 英文原意:write all 语法:wall 信息 3) ...
- Memcache cpu占用过高
分析应该是memcache的内存大小还是默认配置,已经满足不了当前的大数据量的需要了,大量的新缓存需要进入,同时大量的旧缓存又需要被淘汰出来,一进一出导致CPU占用过多.进入注册表,找到:HKEY_L ...
- SpringMVC源码解读 - HandlerMapping - RequestMappingHandlerMapping请求分发
AbstractHandlerMethodMapping实现接口getHandlerInternal,定义查找流程 RequestMappingInfoHandlerMapping根据RequestM ...
- Codeforces828 D. High Load
D. High Load time limit per test 2 seconds memory limit per test 512 megabytes input standard input ...
- 几种String对象方法的区别
1.在String对象方法中,发现.slice()方法和.substring()方法的作用几乎相同,都是根据起始索引返回截取得到的字符串.经过查阅资料和实测得到区别: 正常情况下索引都为正值,返回值为 ...