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的更多相关文章

  1. LeetCode 第 3 题(Longest Substring Without Repeating Characters)

    LeetCode 第 3 题(Longest Substring Without Repeating Characters) Given a string, find the length of th ...

  2. Leetcode经典试题:Longest Substring Without Repeating Characters解析

    题目如下: Given a string, find the length of the longest substring without repeating characters. Example ...

  3. 【LeetCode】3 、Longest Substring Without Repeating Characters

    题目等级:Medium 题目描述:   Given a string, find the length of the longest substring without repeating chara ...

  4. leetcode第三题Longest Substring Without Repeating Characters java

    Longest Substring Without Repeating Characters Given a string, find the length of the longest substr ...

  5. leetcode第三题--Longest Substring Without Repeating Characters

    Problem:Given a string, find the length of the longest substring without repeating characters. For e ...

  6. LeetCode(3)Longest Substring Without Repeating Characters

    题目: Given a string, find the length of the longest substring without repeating characters. For examp ...

  7. LeetCode解题笔记 - 3. Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

  8. LeetCode第三题—— Longest Substring Without Repeating Characters(最长无重复子字符串)

    题目描述 Given a string, find the length of the longest substring without repeating characters. Example ...

  9. LeetCode longest substring without repeating characters 题解 Hash表

    题目 Given a string, find the length of the longest substring without repeating characters. Example 1: ...

随机推荐

  1. Ubuntu安装opencv with cuda

    Ubuntu安装opencv with cuda 为了运行dense flow真是折腾啊,下面网址是教程 http://blog.aicry.com/ubuntu-14-04-install-open ...

  2. 洛谷P3045 [USACO12FEB]牛券Cow Coupons

    P3045 [USACO12FEB]牛券Cow Coupons 71通过 248提交 题目提供者洛谷OnlineJudge 标签USACO2012云端 难度提高+/省选- 时空限制1s / 128MB ...

  3. redis在linux上的安装和配置

    https://blog.csdn.net/lzding/article/details/52040501(直接可以用的安装phpredis) http://www.runoob.com/redis/ ...

  4. JavaScript Array 对象的方法,比如push和unshift

    https://www.runoob.com/jsref/jsref-obj-array.html js数组与字符串的相互转换 一.数组转字符串 需要将数组元素用某个字符连接成字符串,示例代码如下: ...

  5. 【06】Vue 之 组件化开发

    组件其实就是一个拥有样式.动画.js逻辑.HTML结构的综合块.前端组件化确实让大的前端团队更高效的开发前端项目.而作为前端比较流行的框架之一,Vue的组件和也做的非常彻底,而且有自己的特色.尤其是她 ...

  6. 比较全的.NET页面缓存技术文章

    原文发布时间为:2009-11-04 -- 来源于本人的百度文章 [由搬家工具导入] http://www.cnblogs.com/jacksonwj/archive/2009/07/09/15197 ...

  7. EL与OGNL

    EL表达式:  >>单纯在jsp页面中出现,是在四个作用域中取值,page,request,session,application. >>如果在struts环境中,它除了有在上 ...

  8. Codeforces 最大流 费用流

    这套题目做完后,一定要反复的看! 代码经常出现的几个问题: 本机测试超时: 1.init函数忘记写. 2.addedge函数写成add函数. 3.边连错了. 代码TLE: 1.前向星边数组开小. 2. ...

  9. BZOJ2748(DP)

    非常简单的DP题.类似背包的操作,按照音量改变值进行状态转移即可. #include <bits/stdc++.h> using namespace std; #define REP(i, ...

  10. Codeforces 743D Chloe and pleasant prizes(树型DP)

                                                                D. Chloe and pleasant prizes             ...