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: ...
随机推荐
- Hadoop体系所有组件默认端口列表
Why? Hadoop集群组件太多,默认端口无法记住,有事后需要查看,就在这里罗列下这里包含我们使用到的组件:HDFS, YARN, Hbase, Hive, ZooKeeper。 What? 端口 ...
- css 文字垂直居中问题
CSS 文字垂直居中问题 问题:在 div 中文字居中问题: 当使用 line-height:100%%; 时,文字没有居中,如下: html: <div id="header_log ...
- 【11】vue router 之导航钩子
导航钩子 vue-router 提供的导航钩子主要用来拦截导航,让它完成跳转或取消.有多种方式可以在路由导航发生时执行钩子:全局的, 单个路由独享的, 或者组件级的.http://www.jiansh ...
- LeetCode OJ-- Longest Common Prefix
https://oj.leetcode.com/problems/longest-common-prefix/ 在多个string的集合中,找出所有string的最长公共前缀. 从头开始 index ...
- PowerDesigner中如何生成主键和自增列
1.SQL Server版本: 第一步,首先要建立与数据库的连接,方法较多,这里举个例子: http://www.cnblogs.com/netsql/archive/2010/05/17/17375 ...
- 点击添加按钮,使用ajax动态添加一行和移除一行,并且序号重新排序和数据不重复操作判断
<div class="control-group " style="top: -20px;position: relative;"> <la ...
- (21)python lambda表达式
lambda表达式是一个匿名函数 通常是在需要一个函数,但是又不想费神去命名一个函数的场合下使用 最简单的例子 add = lambda x,y : x + y print add(3,5) #out ...
- 51nod 1105 第K大的数 【双重二分/二分套二分/两数组任意乘积后第K大数】
1105 第K大的数 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注 数组A和数组B,里面都有n个整数.数组C共有n^2个整数,分别是A[0] * ...
- 2018年东北农业大学春季校赛 B wyh的矩阵【找规律】
链接:https://www.nowcoder.com/acm/contest/93/B来源:牛客网 题目描述 给你一个n*n矩阵,按照顺序填入1到n*n的数,例如n=5,该矩阵如下 1 2 3 4 ...
- BZOJ 3083 遥远的国度 (换根讨论 + 树链剖分)
题目链接 BZOJ3083 换根不能真正地换. 令当前的根为$cnt$,要查找的子树根为$x$ $1$.$x = cnt$,那么要查找的区域就是整棵树. $2$.$x$在以$cnt$为根的子树内,那 ...