LeetCode Hash Table 3. Longest Substring Without Repeating Characters
HashMap的应用可以提高查找的速度,键key,值value的使用拜托了传统数组的遍历查找方式,对于判断一个字符或者字符串是否已经存在的问题可以非常好的解决。而本题需要解决的问题就是判断新遍历到的字符是否已经存在于左left,右right,字符构成的子串之中。
解题思路:设置一个left作为子串的最左边的字符位置坐标,right不断向右遍历,并对每次遍历得到的字符进行判断,max作为最大的没有重复字符的子串长度。对于right遍历得到的新的字符,有两种情况:
一:在Hash表中没有,此时该字符为第一次出现,肯定不会与之前的字符重复,所以将其(<character,right>)put到Hash表中,将遍历得到的子串长度加一,并判断是否更新max.
二:在Hash表中存在,此时需要判断已经存在的该字母与left的位置关系,如果已经存在的charAt(right)字母在left左侧,说明right字母是在left--right子串中第一次出现,left继续保持不变,更新Hash表中的right字母的位置即可,(因为left 左侧的字母对之后的序列已经不会产生影响了,而left右侧新出现的right字母会影响之后序列的长度判断),增加left--right子串长度,并判断更新max;如果已经存在的right字母在left、或者left的右侧,说明left--right子串中出现了重复的字符,left直接跳到与right字母相同的字母的下一位mid即可(因为l从left到mid之前每一次遍历得到的子串left--right都会存在重复的字符并且长度还在减小),更新hash表中的charAt(right)字符位置信息
代码:
public class Solution {
public int lengthOfLongestSubstring(String s) {
if(s.length() == 0)
return 0;
int left = 0,right = 0,max = 0;
HashMap<Character,Integer> map = new HashMap<Character,Integer>();
for(;right<s.length();right++){
if( map.containsKey(s.charAt(right)) ){
left = Math.max(left,map.get(s.charAt(right))+1 );
}
map.put(s.charAt(right),right); //会将之前出现过的相同字母的坐标覆盖掉
max = Math.max(max,right-left+1);
}
return max;
}
}
没有想法时,可以先使用暴力,他会带你走近题目^_^。
下面记录一下自己第一遍写的代码,每次看到一道题目,第一个也是最常规的想法就是--暴力,通过两层循环(应该说是三层),先找出left--right子串,然后利用java内置函数判断一下,right字母是否存在于left--right子串中。(就当学习一下关于string的函数了)
public class Solution {
public int lengthOfLongestSubstring(String s) {
if(s.length() == 0)
return 0;
char []a = s.toCharArray();
int maxlen = 1;
for(int i = 0;i<a.length-1;i++){
int midlen = 1;
for(int j=i+1;j<a.length;j++){
String sub = s.substring(i,j);
int index = sub.indexOf(a[j]);
if(index == -1){
midlen++;
}
else{
i = i+index;
break;
}
}
if(midlen>maxlen)
maxlen = midlen;
}
return maxlen;
}
}
LeetCode Hash Table 3. Longest Substring Without Repeating Characters的更多相关文章
- LeetCode 第 3 题(Longest Substring Without Repeating Characters)
LeetCode 第 3 题(Longest Substring Without Repeating Characters) Given a string, find the length of th ...
- Leetcode经典试题:Longest Substring Without Repeating Characters解析
题目如下: Given a string, find the length of the longest substring without repeating characters. Example ...
- 【LeetCode】3 、Longest Substring Without Repeating Characters
题目等级:Medium 题目描述: Given a string, find the length of the longest substring without repeating chara ...
- leetcode第三题Longest Substring Without Repeating Characters java
Longest Substring Without Repeating Characters Given a string, find the length of the longest substr ...
- leetcode第三题--Longest Substring Without Repeating Characters
Problem:Given a string, find the length of the longest substring without repeating characters. For e ...
- LeetCode(3)Longest Substring Without Repeating Characters
题目: Given a string, find the length of the longest substring without repeating characters. For examp ...
- LeetCode解题笔记 - 3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- LeetCode第三题—— Longest Substring Without Repeating Characters(最长无重复子字符串)
题目描述 Given a string, find the length of the longest substring without repeating characters. Example ...
- LeetCode longest substring without repeating characters 题解 Hash表
题目 Given a string, find the length of the longest substring without repeating characters. Example 1: ...
随机推荐
- 收集邮票(bzoj 1426)
Description 有n种不同的邮票,皮皮想收集所有种类的邮票.唯一的收集方法是到同学凡凡那里购买,每次只能买一张,并且买到的邮票究竟是n种邮票中的哪一种是等概率的,概率均为1/n.但是由于凡凡也 ...
- poj 1795 DNA Laboratory
DNA Laboratory Time Limit: 5000MS Memory Limit: 30000K Total Submissions: 2892 Accepted: 516 Des ...
- xCode控制台的使用-应用闪退原因跟踪
xCode控制台的使用-应用闪退原因跟踪 今天遇到这个一个问题,使用xCode version = 5.0 编译一个程序,刷到IOS7设备后,应用运行正常,但是刷新到IOS<7,打开饮用后就会出 ...
- js 字符串长度截取
<script> function cutstr(str, len) { var temp, icount = 0, patrn = /[^\x00-\xff]/, strre = &qu ...
- 無法使用 system/bin/r 讀取 pmic pm8937 hardware regitster 的原因
Platform Qualcomm MSM8917 + PM8937 + PMI8940 起因 同事問我 PM8937 的 VREG_L17 如何設定成 3.3V, 從 PM8937 hardware ...
- Python_代码练习_写一个判断是否为小数的函数
这两天在学习函数,练习写一个判断是否为小数的函数,看起来蛮简单的,飞速写完很是得意,然后测了一下,发现差得好多呀,这个并不像想象那样简单,我得到的教训是,想要把一个需求哪怕再小的需求考虑周全,都不是件 ...
- Write a function that generates one of 3 numbers according to given probabilities
You are given a function rand(a, b) which generates equiprobable random numbers between [a, b] inclu ...
- webpack常用配置项配置文件介绍
一.webpack基础 1.在项目中生成package.json:在项目根目录中输入npm init,根据提示输入相应信息.(也可以不生成package.json文件,但是package.json是很 ...
- Oracle 安装前准备
[root@localhost Desktop]# groupadd -g 110 oinstall 用来安装oracle软件 [root@localhost Desktop]# groupadd - ...
- iOS 5 does not allow to store downloaded data in Documents directory? ios5.0及以后的版本对于下载的文件存储路径有了改变
I have made an application for my client by keeping target iOS as 4. But since the application still ...