题意很简单,就是寻找一个字符串中连续的最长包含不同字母的子串。

其实用最朴素的方法,从当前字符开始寻找,找到以当前字符开头的最长子串。这个方法猛一看是个n方的算法,但是要注意到由于字符数目的限制,其实这是个O(Cn)的算法,最长也不过是C长度。所以我觉得普通方法应该是能过的。

于是写了一个,字符数目最大也不超过256所以代码如下:

 class Solution {
public:
int lengthOfLongestSubstring(string s) { int res=;
for(int i=;i<s.length();i++)
{
int flag[];
memset(flag,,sizeof(flag));
int count = ;
flag[s[i]-' '] = ;
int tempres = ;
for (int j = i + ; j < s.length(); j++) {
if (flag[s[j]-' ']==) {
flag[s[j]-' ']= ;
tempres++;
} else
break;
}
if (tempres > res)
res=tempres;
}
return res;
}
};

其中空格是ASCII码第一个实际意义字符,所以减去‘ ’

正常的O(n)方法是使用哈希表来存已经出现的字符,使用一个指针依次检索,如果碰到已经存在的字符,则去使用第二个指针更新这个哈希表。

从原理上来具体讨论双指针这个算法,对于第一个指针指到的字母有两种情况:

1.从来没有使用过

当前长度加一,将这个位置和字母加入哈希表,第二个指针不动

2.已经使用过,并且有一个哈希表存这个字母的上一个位置

获取上一个位置右侧位置(+1操作),将这个位置和第二个指针比较,

(1)如果小于第二个指针,说明以当前结尾的字符串在第j个指针的地方有字母重复,忽略这个位置,继续以j为准

(2)如果大于第二个指针,说明以当前结尾的字符串在这个最新的位置有重复,将指针移到这个位置,计算这个长度。

这三种情况涵盖了所有可能,并在下面的例子中有相应出现。

代码:

此代码来自最多discuss区最多vote的答案:https://leetcode.com/discuss/23883/11-line-simple-java-solution-o-n-with-explanation

 public int lengthOfLongestSubstring(String s) {
if (s.length()==0) return 0;
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
int max=0;
for (int i=0, j=0; i<s.length(); ++i){
if (map.containsKey(s.charAt(i))){
j = Math.max(j,map.get(s.charAt(i))+1);
}
map.put(s.charAt(i),i);
max = Math.max(max,i-j+1);
}
return max;
}

例子:

对于这个字符串:abcbcda

经过初始化循环执行过程如下:

i=0;  j=0;  (a,0)  max(0,1)=1  第一种情况

i=1;  j=0;  (b,1)  max(1,2)=2  同上

i=2;  j=0;  (c,2)  max(2,3)=3  同上

i=3;  j=(0,1+1)=2;  (b,3)  max(3,2)=3  第二种第二个情况

i=4;  j=(2,2+1)=3;  (c,4)  max(3,2)=3  同上

i=5;  j=3;  (d,5)  max(3,3)=3  第一种情况

i=6;  j=(3,1)=3;   (a,6)  max(3,4)=4  第二种第一个情况

   

Longest Substring Without Repeating Characters - 哈希与双指针的更多相关文章

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

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

  2. [LeetCode_3] Longest Substring Without Repeating Characters

    LeetCode: 3. Longest Substring Without Repeating Characters class Solution { public: int lengthOfLon ...

  3. Longest Substring Without Repeating Characters (c#)

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

  4. LeetCode3:Longest Substring Without Repeating Characters

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

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

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

  6. C++版- Leetcode 3. Longest Substring Without Repeating Characters解题报告

    Leetcode 3. Longest Substring Without Repeating Characters 提交网址: https://leetcode.com/problems/longe ...

  7. 3.Longest Substring Without Repeating Characters(string; HashTable)

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

  8. 蜗牛慢慢爬 LeetCode 3. Longest Substring Without Repeating Characters [Difficulty: Medium]

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

  9. LeetCode第[3]题(Java):Longest Substring Without Repeating Characters 标签:Linked List

    题目中文:没有重复字符的最长子串 题目难度:Medium 题目内容: Given a string, find the length of the longest substring without ...

随机推荐

  1. C#中隐式操作CMD命令行窗口

    原文:C#中隐式操作CMD命令行窗口 MS的CMD命令行是一种重要的操作界面,一些在C#中不那么方便完成的功能,在CMD中几个简单的命令或许就可以轻松搞定,如果能在C#中能完成CMD窗口的功能,那一定 ...

  2. 解决Flex4 发布后访问 初始化极其缓慢的问题

    原文http://blog.163.com/vituk93@126/blog/static/170958034201282222046364/ 昨天找了个免费.net空间,想测试一下做的一个简单Fle ...

  3. Spring Boot的一个测试用例

    package tk.mybatis.springboot.mapper; import org.junit.Assert; import org.junit.Test; import org.jun ...

  4. Optipng—PNG的优化图像工具初探

    PNG 即 Portable Network Graphic 的简称,PNG 图像是一种无损压缩图像文件格式.因为网络传输的需要,我们总是希望 PNG 图像的容量能够小些.小些.再小些.要优化 PNG ...

  5. hdu 4034 Graph(逆向floyd)

    floyd的松弛部分是 g[i][j] = min(g[i][j], g[i][k] + g[k][j]);也就是说,g[i][j] <= g[i][k] + g[k][j] (存在i-> ...

  6. linux中find批量删除空文件及空文件夹

    linux下批量删除空文件(大小等于0的文件)的方法  代码如下 复制代码 find . -name "*" -type f -size 0c | xargs -n 1 rm -f ...

  7. 精通 VC++ 实效编程280例 - 01 窗口

    窗口是屏幕上的一个矩形区域.窗口分为3种:重叠窗口.弹出窗口和子窗口.每个窗口都有由系统绘制的“非客户区”和应用程序绘制的“客户区”.在 MFC 中,CWnd 类为各种窗口提供了基类. 1 通过 HW ...

  8. HDU 5769 Substring(后缀数组)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5769 [题目大意] 在一个串中求出包含字母的子串个数, 只要存在一个字符不相等的子串即可视为不同的 ...

  9. Uber到底挣钱不挣钱,听听司机怎么说

    “根本不挣钱.”Joel开着他崭新的丰田凯美瑞混合动力车,在硅谷早晨的濛濛细雨中,不无幽怨地说.Joel是一名Uber司机,从坐上他车的那一刻起,他的抱怨便一直没停过:“过去日子还挺好过,一个月靠开U ...

  10. Android 架构【转】

    import java.util.ArrayList; import java.util.List;   import android.app.Activity; import android.app ...