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 ...
随机推荐
- 【UOJ】【34】多项式乘法
快速傅里叶变换模板题 算法理解请看<算法导论>第30章<多项式与快速傅里叶变换>,至于证明插值唯一性什么的看不懂也没关系啦-只要明白这个过程是怎么算的就ok. 递归版:(425 ...
- android控件---spinner
spinner下拉列表框的列表项有两种配置方式: 1.通过资源文件配置,通过在values种的xml,比如strings.xml中使用<string-array>元素添加制定列表项内容,然 ...
- .net 对称加密DESCryptoServiceProvider
1.生成密钥以加密和解密数据 DESCryptoServiceProvider 基于一种对称加密算法.对称加密需要密钥和初始化矢量 (IV) 来加密数据.要解密该数据,您必须拥有此同一密钥和 IV.您 ...
- hdu 1757 A Simple Math Problem (矩阵快速幂,简单)
题目 也是和LightOJ 1096 和LightOJ 1065 差不多的简单题目. #include<stdio.h> #include<string.h> #include ...
- 2013 Multi-University Training Contest 1 3-idiots
解题报告: 记录 A_i 为长度为 i 的树枝的数量,并让 A 对它本身做 FFT,得到任意选两个树枝能得到的各个和的数量.枚举第三边, 计算出所有两边之和大于第三条边的方案数,并把前两条边包含最长边 ...
- SQO2008配置管理工具服务显示远程过程调用失败
前两天,装了VS2012后,打开SQL2008配置管理工具,发现SQL服务名称里什么也没有,只有一个提示:(如图) 卸载了一个叫"Microsoft SQL Server 2012Local ...
- 毕向东JAVA视频讲解(四五课)
内存的划分: 1,寄存器. 2,本地方法区. 3,方法区. 4,栈内存. 存储的都是局部变量. 而且变量所属的作用域一旦结束,该变量就自动释放. 5,堆内存. 存储是数组和对象(其实数组就是对象) 凡 ...
- LAMP环境 源码包安装
linux的学习很早就开始了,大学的时候的时候有有学过unix,后来每年都有去看看linux,因为在小城市的缘故,很少会实际工作中用到,基本都是智慧云之类的,同事也说,你学起来也用不上,IT生态不好, ...
- mysql表的一对一/一对多/多对多联系
1.数据库中的多对多关联关系一般需采用中间表的方式处理,将多对多转化为两个一对多. 2.通过表的关系,来帮助我们怎样建表,建几张表. 一对一 一张表的一条记录一定只能与另外一张表的一条记录进行对应,反 ...
- Sina App Engine(SAE)入门教程(5)- SaeSegment(中文分词服务)使用
分词能干什么? 提取一篇文章的关键字 检测特定的段落中有没有违禁词 智能机器人 …..尽你所想 开启SAE 分词服务 首先你需要在sae的管理面板开始分词服务后才能使用sae的服务.具体的开启操作: ...