Java [leetcode 3] Longest Substring Without Repeating Characters
问题描述:
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.
解题思路:
思路与第一题有点类似,同样是用HashMap来存储数组值和下标,其中数组值为HashMap中的key值,数组下标为HashMap中的value值。不同的是,这次需要一个类似于C++中的指针来指明字串的起点。也就是说,每次添加一个key值时,是从指针往后的范围比较是否重复的,如果重复的话则将指针移动到该值下一个下标处,否则往HashMap中继续添加键值并往后比较。
代码如下:
public class Solution {
public int lengthOfLongestSubstring(String s) {
Map<Character, Integer> map = new HashMap<Character, Integer>();
int length = 1;
int sum = 0;
int pointer = 0; if (s == null || s.length() == 0)
return 0; for (int i = 0; i < s.length(); i++) {
if (map.containsKey(s.charAt(i)) && map.get(s.charAt(i)) >= pointer) {
Integer tmp = map.get(s.charAt(i));
sum = i - pointer;
length = Math.max(sum, length);
pointer = tmp + 1;
map.put(s.charAt(i), i); } else {
map.put(s.charAt(i), i);
}
} length = Math.max(s.length() - pointer, length); return length;
}
}
Java [leetcode 3] Longest Substring Without Repeating Characters的更多相关文章
- C++版- Leetcode 3. Longest Substring Without Repeating Characters解题报告
Leetcode 3. Longest Substring Without Repeating Characters 提交网址: https://leetcode.com/problems/longe ...
- 【JAVA、C++】LeetCode 003 Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
- [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(最长不重复子序列)
题目来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, f ...
- LeetCode 3 Longest Substring Without Repeating Characters 解题报告
LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...
- leetcode:Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
- [LeetCode][Python]Longest Substring Without Repeating Characters
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...
- 蜗牛慢慢爬 LeetCode 3. Longest Substring Without Repeating Characters [Difficulty: Medium]
题目 Given a string, find the length of the longest substring without repeating characters. Examples: ...
- LeetCode之Longest Substring Without Repeating Characters
[题目描述] Given a string, find the length of the longest substring without repeating characters. Exampl ...
随机推荐
- trie树(前缀树)
问题描述: Trie树,即字典树,又称单词查找树或键树,是一种树形结构,是一种哈希树的变种.典型应用是用于统计和排序大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计.它的优 ...
- 2014 Multi-University Training Contest 3
官方解题报告http://blog.sina.com.cn/s/blog_a19ad7a10102uyiq.html Wow! Such Sequence! http://acm.hdu.edu.cn ...
- 宏基5750G 不能用内置无线网卡上网
宏基5750G 不能用内置无线网卡上网 具体体现在: 1.--> 搜索不到无线网络: 2.点击“打开网络和共享中心”-->没有“管理无线网络”一项.如果正常的话应该是这样的: 3.“我的电 ...
- jquery 父页面 子页面 同级页面 调用
项目中用到 代码 ,先 展示 ,以便以后用到,直接看下就会明白. var li_divs = parent.$("#servicelist")[0].contentWindow. ...
- Using command-line Subversion to access project source files
Help index About source code version control with Software Configuration Management (Subversion) Usi ...
- structs spring hibernate 三者之间有什么关系?
现在开发流行MVC模式,structs在C(控制器)中使用:hibernate在M(模型)中被使用:至于 spring ,最大的作用在于,structs.hibernate的对象,由于在各个层之间相互 ...
- Spring @ Component 的作用
1.@controller 控制器(注入服务) 2.@service 服务(注入dao) 3.@repository dao(实现dao访问) 4.@component (把普通pojo实例化到spr ...
- poj 3101 Astronomy
2个星球周期为a,b.则相差半周的长度为a*b/(2*abs(a-b)),对于n个只需求这n个 分数的最小公倍数即可! 公式: 分数的最小公倍数 = 分子的最小公倍数/分母的最大公约数 由于涉及到大数 ...
- zend studio 10破解/汉化(转发)
转发:http://blog.csdn.net/qq1355541448/article/details/16807429 Zend Studio 10正式版破解及汉化 2013年03月12日 ⁄ P ...
- SaaS系列介绍之十一: SaaS商业模式分析
1 配置模式 中国企业很多是人治,管理弹性非常大,公司的政策经常变化,管理流程.业务变化也非常大,发展也非常快;一个公司今年是10个人,明年是100个人,后年可能是1000人.管理机制.方法处于经常变 ...