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, ...
随机推荐
- Windows下PythonQt3.2使用pandas.pivot_table
本机环境 1.win7 64 旗舰版 2.Qt 5.9.1(MSVC 2015,32 bit) 3.Python 3.7.1 (32-bit),二进制包安装的,即Windows x86 execut ...
- BZOJ1433或洛谷2055 [ZJOI2009]假期的宿舍
BZOJ原题链接 洛谷原题链接 对于每个需要床位的人向他能睡的床连边,然后就是二分图最大匹配模板了. 这里用匈牙利算法. #include<cstdio> #include<cstr ...
- powershell获取windows子文件夹的大小
$startFolder = "E:\Migration\" $colItems = (Get-ChildItem $startFolder | Where-Object {$_. ...
- 开始Dev之路
从今天开始,开启Dev的发展之路.
- No write since last change (add ! to override)
故障现象: 使用vim修改文件报错,系统提示如下: E37: No write since last change (add ! to override) 故障原因: 文件为只读文件,无法修改. 解决 ...
- zookeeper 服务挂掉重启后,dubbo 服务是不会自动重新注册上的
今天遇到一个问题: 系统初始有两个dubbo 服务 , A 和 B , 都是正常注册到zookeeper 上的, 但是zookeeper 服务机房 断电导致 服务宕机, 那就重启吧. 一切正常. 但是 ...
- python_paramiko
目录: paramiko模块介绍 paramiko模块安装 paramiko模块使用 一.paramiko模块介绍 paramiko是一个用于做远程控制的模块,使用该模块可以对远程服务器进行命令或文件 ...
- easy ui Uncaught Error: cannot call methods on tooltip prior to initialization; attempted to call method 'hide'
今天bootstrap 和easy ui混用时候报了这么一个错误,本来可以点击编辑的,但是现在一点击就报错,
- CCPC-2017-秦皇岛站
10月25日 听说信用卡到了好兴奋,然而没有额度是啥情况啊qwq. 晚上坐飞机出发,成都-鄂尔多斯-石家庄-秦皇岛,队友吐槽鄂尔多斯到石家庄好近啊,然后过了一会儿我们因为石家庄大雾迫降在了济南.嘤嘤嘤 ...
- HTML中Meta标签中http-equiv属性小结
HTML中Meta标签中http-equiv的用法: <meta http-equiv="这里是参数" content="这里是参数值"> 1.Ex ...