这题的题意大概就是给你一个字符串"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. [2014-03-13 08:46:42 - Dex Loader] Unable to execute dex: java.nio.BufferOverflowException. Check the Eclipse log for stack trace.

    Unable to execute dex: java.nio.BufferOverflowException. Check the Eclipse log for stack trace. 问题提示 ...

  2. Java面试汇总

    转自:http://zy19982004.iteye.com/blog/1846537#comments 一.All 最近找工作,遇到的笔试面试题,归纳如下,供大家参考. 二.J2SE 容器 Hash ...

  3. [HIHO1328]逃离迷宫(bfs,位压)

    题目链接:http://hihocoder.com/problemset/problem/1328 这个题bfs到时候不止要存当前的坐标,还要存当前有哪几把钥匙.因为5把钥匙,所以可以直接用位来存,这 ...

  4. leetcode:Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  5. Storm安装与实验

    接上一篇Kafka的安装与实验: http://www.cnblogs.com/charlesblc/p/6046023.html 还有再上一篇Flume的安装与实验: http://www.cnbl ...

  6. WebView中Js与Android本地函数的相互调用

    介绍 随着Html5的普及,html在表现力上不一定比原生应用差,并且有很强的扩展兼容性,所以越来越多的应用是采用Html与Android原生混合开发模式实现. 既然要实现混合开发,那么Js与Andr ...

  7. 最全的PHP常用函数大全

    PHP的一些常用函数 quotemeta() 函数在字符串中某些预定义的字符前添加反斜杠. quoted_printable_decode() 函数对经过 quoted-printable 编码后的字 ...

  8. 利用RunTime解决由NSTimer导致的内存泄漏

    NSTimer使用场景 用NSTimer来实现每隔一定时间执行制定的任务,例如最常见的广告轮播图,使用NSTimer实现这个功能很简单代码如下 NSTimer *_timer; _timer = [N ...

  9. HDU 2126 (背包方法数) Buy the souvenirs

    DP还有很长很长一段路要走.. 题意:给出n纪念品的价格和钱数m,问最多能买多少件纪念品和买这些数量的纪念品的方案数. 首先,求能买最多的纪念品的数量,用贪心法可以解决.将价钱排序,然后从最便宜的开始 ...

  10. 51nod1244 莫比乌斯函数之和

    推公式.f[n]=1-∑f[n/i](i=2...n).然后递归+记忆化搜索.yyl说这叫杜教筛?时间复杂度貌似是O(n 2/3)的? #include<cstdio> #include& ...