题目链接

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.


推荐参考博客:最长不重复子串

注意:输入空串时,返回0

以abcdbefdhij 为例:

图中的start是一个标志位,表示当前不重复子串的起始位置,图中的数字表示记录字符出现位置的数组hashtable,比如字符b出现在第1位,那么hashtable[‘b’]=1。                              本文地址

顺序扫描字符串,第4个位置时,在hashtable中发现b已经出现过(记出现的位置为k,此时k=1),那么当前的不重复子串长度 = 当前位置-start;下一个不重复子串就应该从第k+1个字符(2号位的c)开始,即令start = 2,并且把[start, k)位置的字符对应的hashtable清空,重新设置b在hashtable的位置为4。继续扫描直到再次发现相同的字符,和前面一样处理。注意全部处理完字符串,还要判断一下末尾的不重复子串是否是最长的。

时间复杂度分析:最坏情况下,相当于遍历了两遍字符串,因此时间复杂度是O(n)

class Solution {
public:
int lengthOfLongestSubstring(string s) {
vector<int> bitmap(128, -1);
int res = 0;
int start = 0, lastStart = 0;
for(int i = 0; i < s.size(); i++)
{
if(bitmap[s[i]] != -1)
{
res = max(res, i-start);
lastStart = start;
start = bitmap[s[i]] + 1;
for(int j = lastStart; j < bitmap[s[i]]; j++)
bitmap[s[j]] = -1;
}
bitmap[s[i]] = i;
}
res = max(res, (int)s.size()-start);//不要忘了最后的判断
return res;
}
};

【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3736725.html

LeetCode:Longest Substring Without Repeating Characters(最长不重复子串)的更多相关文章

  1. [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串

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

  2. LeetCode Longest Substring Without Repeating Characters 最长不重复子串

    题意:给一字符串,求一个子串的长度,该子串满足所有字符都不重复.字符可能包含标点之类的,不仅仅是字母.按ASCII码算,就有2^8=128个. 思路:从左到右扫每个字符,判断该字符距离上一次出现的距离 ...

  3. [LeetCode] Longest Substring Without Repeating Characters最长无重复子串

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

  4. [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串

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

  5. [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串 C++实现java实现

    最长无重复字符的子串 Given a string, find the length of the longest substring without repeating characters. Ex ...

  6. [LeetCode] 3.Longest Substring Without Repeating Characters 最长无重复子串

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

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

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

  8. leetcode 3 Longest Substring Without Repeating Characters最长无重复子串

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

  9. 003 Longest Substring Without Repeating Characters 最长不重复子串

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

  10. Longest Substring Without Repeating Characters 最长不重复子串

    只遍历一次字符串即可求出最长不重复子串的长度. int lengthOfLongestSubstring(string s) { vector<,-); //记录字符上一次出现的位置,ASCII ...

随机推荐

  1. 日志log使用序列反序列加密(Serializer) DESCrypto 加密

    若一次加密一个文件内容,文件内容不会更新变化,网上大多数序列化反序列加密程序是没问题的. 1:由于log文件的随时会更新内容,那网上常用的程序是行不通的.需要做修改 若想通过打开txt , using ...

  2. iOS删除本地文件

    以前在博客里记录的东西都是截屏,没有插入代码,今天进去一看,图片都不显示了,只好重新插入代码,发现以前写的文件操作这块,没有写本地文件删除这个功能,重新再记录一下 //需要删除文件的物理地址 NSSt ...

  3. Response、Request、QueryString,repeater添加,修改,删除数据

    内置对象: Response对象:响应请求,Response对象用于动态响应客户端请示,控制发送给用户的信息,并将动态生成响应.Response.Write("<script>a ...

  4. Okhttp https

    1. 绕过CA证书,不建议使用 private void ingoreCA() throws NoSuchAlgorithmException, KeyManagementException { SS ...

  5. Java复数的四则运算

    import java.util.Scanner;   import com.sun.jndi.url.iiopname.iiopnameURLContextFactory;   public cla ...

  6. jquery checkbox选中、改变状态、change和click事件

    jquery判断checked的三种方法:.attr('checked); //看版本1.6+返回:”checked”或”undefined” ;1.5-返回:true或false.prop('che ...

  7. String reorder

    本问题出自:微软2014实习生及秋令营技术类职位在线测试 (Microsoft Online Test for Core Technical Positions) Description For th ...

  8. jquery实现TODOList

    html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <titl ...

  9. 怎么定义 logger

    随便打开一个 spring 的 源文件,比如  PathMatchingResourcePatternResolver.class 里面是这样定义logger 的 import org.apache. ...

  10. 「2013-9-14」Change Remote Desktop Port

    修改远程桌面服务(Remote Desktop Service)的端口号,有几点原因: 默认是 3389 端口,也是经常被端口嗅探器扫描的一个端口.更换端口号,一定程度上可以带来更好的安全性. 如果一 ...