这题的题意大概就是给你一个字符串"abcdecde",找到最长的子字符串长度,里面所有的子母都不重复。本例子中最长的满足条件的子字符串就是"abcde",所以应该返回的是5。这一题如果不用暴力解决的方法的话,我优先想到的数据结构是HashMap,因为它能够判断字母是否重复,并且记录每个字母的index。而现在唯一的难点就是,怎么通过HashMap来找到这个最长字符串。因为要记录子字符串的长度,从而,肯定需要两个index,一个index1作为子字符串的头,另一个index2再往后移动,直到index2碰到index1和index2之间出现过的元素(假设位置为i),则记录length = index2 - index1(不需要加1是因为index2是重复元素,所以得以index2-1来算),同时,要把HashMap里面index1和i之间的元素给删掉,index1从i+1开始,index2继续走下去。每一次记录length,都要判断是否比之前最长的length要长,如果是,就更新它,不是,就跳过。

  这里还有一个边界条件。就是,当index2走完整个字符串的时候,length并不会被记录,所以在循环外得判断一遍。

  代码如下:

public class Solution {
public int lengthOfLongestSubstring(String s) {
if (s.length() == 0 || s.length() == 1) return s.length();
HashMap<Character,Integer> map = new HashMap<Character, Integer>();
//length为最大长度,head为当前子字符串的头指针。
int length = 0;
int head = 0;
//把第一个字母放到map里。
map.put(s.charAt(head),head);
for (int i = 1; i < s.length(); i++) {
char c = s.charAt(i);
//contain的话,说明i和head之间存在与c相同的元素。
if (map.containsKey(c)) {
//找出重复元素的位置index。并去掉map里面index前所有的字符(包括该字符)
int index = map.get(c);
for (int j = head; j <= index; j++) {
map.remove(s.charAt(j));
}
//记录并比较当前最长的length
length = Math.max(length,i - head);
//head从index+1开始(index和i重复,所以从index+1开始)
head = index+1;
}
map.put(c,i);
}
//最后再判断前面说的边界条件,上面循环并没有记录i走到字符串尾时的子字符串长度。
if (head != s.length() - 1) {
length = Math.max(length, s.length() - head);
}
return length;
}
}

LeetCode(3) - Longest Substring Without Repeating Characters的更多相关文章

  1. 【一天一道LeetCode】 #3 Longest Substring Without Repeating Characters

    一天一道LeetCode (一)题目 Given a string, find the length of the longest substring without repeating charac ...

  2. 【LeetCode OJ】Longest Substring Without Repeating Characters

    题目链接:https://leetcode.com/problems/longest-substring-without-repeating-characters/ 题目:Given a string ...

  3. 【LeetCode】3. Longest Substring Without Repeating Characters 无重复字符的最长子串

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:无重复字符,最长子串,题解,leetcode, 力扣,py ...

  4. 【LeetCode】3.Longest Substring Without Repeating Characters 最长无重复子串

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

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

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

  6. leetcode题解 3. Longest Substring Without Repeating Characters

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

  7. 【LeetCode】3. Longest Substring Without Repeating Characters (2 solutions)

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

  8. 《LeetBook》leetcode题解(3):Longest Substring Without Repeating Characters[M]——哈希判断重复

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  9. LeetCode OJ:Longest Substring Without Repeating Characters(最长无重复字符子串)

    Given a string, find the length of the longest substring without repeating characters. For example, ...

随机推荐

  1. Android中的sp与wp

    一.相关code文件 二.code具体分析 lightrefebase: refbase: sp: wp: flag: 三.使用注意事项 不能在把目标对象赋给一个长久存在的sp对象之前赋给一个短生命周 ...

  2. java-基础练习题3

    [程序3] 题目:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身.例如:153是一个"水仙花数",因 ...

  3. [HIHO]hihoCoder太阁最新面经算法竞赛7

    题目链接:http://hihocoder.com/contest/hihointerview12 期末完事了,终于有时间成套刷题了.这套题比较简单,难度上感觉和上一套差不多.除了最后一个题是看了讨论 ...

  4. Trianglify – 五彩缤纷的 SVG 背景图案

    Trianglify 是一个能够生成五颜六色的三角形图案的 JavaScript 库,可以用来作为 SVG 图像和 CSS 背景.它的灵感来自于 Btmills 的 Geopattern,并使用 d3 ...

  5. poj 1195 mobile phone

    题目连接: 题意:要求设计这样一个数据结构,支持下列操作 1.add(x,y,a).对二维数组的第x行,第y列加上a. 2.sum(l,b,r,t).求所有满足l<=x<=r,b<= ...

  6. [Warning] TIMESTAMP with implicit DEFAULT value is deprecated

    As indicated by the warning, to turn off the nonstandard behaviors, enable the new explicit_defaults ...

  7. ubuntu 11.04 源 更新不了,全显示ign、404

    原文地址:http://blog.csdn.net/enjio/article/details/11603373   ubuntu 11.04 源 更新不了 分类: 开发相关2013-09-12 14 ...

  8. BZOJ 3295 动态逆序对

    调了好久.... 转化成三维偏序,cdq处理. 好像比较快? #include<iostream> #include<cstdio> #include<cstring&g ...

  9. /bin/bash: [xxxx]: command not found

    /******************************************************************************** * /bin/bash: [xxxx ...

  10. vim 常用命令总结

    https://www.ibm.com/developerworks/cn/linux/l-cn-tip-vim/ 1 文档开头末尾 [[ 开头 ]]末尾 2 删除全部 :%d 3 剪切.复制.粘贴 ...