3. [leetcode] 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.
Solutions
HashMap + two pointers
public int lengthOfLongestSubstring(String s) {
if(s == null || s.length() == 0){
return 0;
}
Map<Character, Character> map = new HashMap();
int j = 0;
int max = 0;
char[] ch = s.toCharArray();
for(int i = 0; i <ch.length; i++){
while(j < ch.length && !map.containsKey(ch[j])){
map.put(ch[j], ch[j]);
j++;
if(j - i > max){
max = j - i;
}
}
map.remove(ch[i]);
}
return max;
}
3. [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, ...
随机推荐
- SSM-Spring-18:Spring中aspectJ的XML版
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- aspectJ的xml版是开发中最常用的: 下面直接已案例入手,毕竟繁琐的日子不多了 案例:两个接口,俩个实现 ...
- SSM-MyBatis-02:Mybatis最基础的增删改查(查全部和查单独一个)
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 继续上次的开始,这次记录的是增删改查,上次重复过的代码不做过多解释 首先先创建mysql的表和实体类Book ...
- Spring-Security-OAuth2调用微信API
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.an ...
- GitHub 系列之「Git速成」
1.什么是Git? Git 是 Linux 发明者 Linus 开发的一款新时代的版本控制系统,那什么是版本控制系统呢?怎么理解?网上一大堆详细的介绍,但是大多枯燥乏味,对于新手也很难理解,这里我只举 ...
- scala 访问阿里云oss
我们的数据一天就一个T,数据量不断增大,集群磁盘有限,所以把冷数据放到了oss,偶尔会使用到冷数据,如果使用的时候还的从oss上拉数据这样很浪费时间后来想了个办法可以直接获取到oss上的数据.案例:o ...
- 打包前端WebSite到Go程序
打包前端WebSite到Go程序 Coolpy5发布在即,新版本要求服务端程序只是一个运行文件,经历了go的template无数坑后,最后还是放弃了,所以还是要把前端独立开发一个纯前端程序,但是go程 ...
- Python3 ——斐波那契数列(经典)
刚刚学习了 斐波那契数列,整理一下思路,写个博文给未来的学弟学妹参考一下,希望能够帮助到他们 永远爱你们的 ----新宝宝 经历过简单的学习之后,写出一个比较简单的代码,斐波那契数列:具体程序如下: ...
- Appium 【已解决】提示报错:Attempt to re-install io.appium.android.ime without first uninstalling.
详细报错:Failed to install D:\AutoTest\appium\Appium\node_modules\appium\build\unicode_ime_apk\UnicodeIM ...
- 如何提高使用Java反射的效率?
前言 在我们平时的工作或者面试中,都会经常遇到“反射”这个知识点,通过“反射”我们可以动态的获取到对象的信息以及灵活的调用对象方法等,但是在使用的同时又伴随着另一种声音的出现,那就是“反射”很慢,要少 ...
- 阿里云大数据计算服务 - MaxCompute (原名 ODPS)
MaxCompute 是阿里EB级计算平台,经过十年磨砺,它成为阿里巴巴集团数据中台的计算核心和阿里云大数据的基础服务.去年MaxCompute 做了哪些工作,这些工作背后的原因是什么?大数据市场进入 ...