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.

思路:

关键是记录当前子串的起始位置和用hash表记录每个字母出现的情况。

大神简约版的代码:

class Solution {
public:
int v[]; //asciib码最多256个
int lengthOfLongestSubstring(string s) {
memset(v,-,sizeof(v)); //位置先全部赋值为-1
int start = , ans = ;
for (int i = ; i < s.size(); ++i) {
if (v[s[i]] >= start) { //如果当前子串中已经出现了该字符 更新答案和起始位置
ans = ans > i - start ? ans : i - start;
start = v[s[i]] + ;
}
v[s[i]] = i;
}
ans = ans > s.size() - start ? ans : s.size() - start;
return ans;
}
};

思路是一样的,我自己好长好慢的代码:

int lengthOfLongestSubstring(string s) {
unordered_map<char, int> v;
int ans = ;
int len = ;
int start = ; //记录当前子串的起始位置
for(int i = ; i < s.size(); i++)
{
if(v.find(s[i]) == v.end())
{
len++;
v[s[i]] = i;
}
else
{
string tmp = s.substr(i - len, len);
ans = (len > ans) ? len : ans;
len = i - v[s[i]];
for(int j = start; j < v[s[i]]; j++) //把重复字符前面的字符次数都置0
{
v.erase(s[j]);
}
start = v[s[i]] + ;
v[s[i]] = i; }
}
ans = (len > ans) ? len : ans;
return ans;
}

【leetcode】Longest Substring Without Repeating Characters (middle)的更多相关文章

  1. 【LeetCode】Longest Substring Without Repeating Characters 解题报告

    [题意] Given a string, find the length of the longest substring without repeating characters. For exam ...

  2. 【leetcode】Longest Substring Without Repeating Characters

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

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

    这道题是LeetCode里的第3道题. 题目描述: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: ...

  4. [Leetcode] 3.Longest Substring Without Repeating Characters(unordered_map)

    通过把未访问的结点放到unordered_map中来判断是否重复,代码如下: class Solution { public: int lengthOfLongestSubstring(string ...

  5. 【Leetcode】【Medium】Longest Substring Without Repeating Characters

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

  6. LeetCode 3 Longest Substring Without Repeating Characters(最长不重复子序列)

    题目来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, f ...

  7. 【LeetCode3】Longest Substring Without Repeating Characters★★

    题目描述: 解题思路: 借用网上大神的思想:the basic idea is, keep a hashmap which stores the characters in string as key ...

  8. LeetCode #003# Longest Substring Without Repeating Characters(js描述)

    索引 思路1:分治策略 思路2:Brute Force - O(n^3) 思路3:动态规划? O(n^2)版,错解之一:420 ms O(n^2)版,错解之二:388 ms O(n)版,思路转变: 1 ...

  9. 「LeetCode」0002-Longest Substring Without Repeating Characters(C++)

    分析 贪心思想.注意更新每次判断的最长不同子串的左区间的时候,它是必须单调增的(有时候会在这里翻车). 代码 关掉流同步能有效提高速度. static const auto io_sync_off = ...

随机推荐

  1. Mysql增加主键或者更改表的列为主键的sql语句

                                                                                                        ...

  2. Linux命令(5):cp命令

    1.作用: 将给出的文件或目录复制到另一文件或目录中: 2.格式: cp  [选项] 源文件或目录 目标文件或目录. 3.常见参数: 4.使用实例: [root@localhost ~]# cp -a ...

  3. 返璞归真vc++之感言

    本人自述,大专学历,感觉自己也属于好学型学生,历任班上学习委员3年有余,参与学校项目几多个,不知道不觉从11年毕业已有3个年头,3年来,不敢苟同自己的生活方式,奈何人生无奈..从刚开始的电子商务公司转 ...

  4. libnet发包例子(tcp udp arp广播)

    #include <libnet.h> int main() { libnet_t *handle; /* Libnet句柄 */ int packet_size; /* 构造的数据包大小 ...

  5. 利用LRUMap 设计缓存

    上下文缓存,即将一些常用的数据至于一个缓存集合中,当需要获取这些数据的时候,直接从缓存中读取,而不必每次都从数据库读取,以此提高效率. 其原理类似Apache Commons Collections  ...

  6. Demo学习: ColumnSort

    ColumnSort 设置UniDGGrid点击表头时排序,设置方法比较麻烦且不通用,在实际开发中用处不大. 自己在项目中用了一个比较笨的办法,写了一个函数通过sql来排序: procedure TM ...

  7. hadoop中遇到的问题。

    1.物理主机中无法访问管理界面,在虚拟主机中可以访问, 这跟防火墙有关系,重启一下防火墙,然后关闭,最后重启一下handoop,应该就可以了!!!!(hadoop首战顺利!!!!!(●'◡'●))

  8. chown

    chown 命令 用途:更改与文件关联的所有者或组 chown [ -f ] [ -h ] [ -R ] Owner [ :Group ] { File ... | Directory ... } c ...

  9. 【 socke】C# socket端口复用-多主机头绑定

    什么是端口复用: 因为在winsock的实现中,对于服务器的绑定是可以多重绑定的,在确定多重绑定使用谁的时候,根据一条原则是谁的指定最明确则将包递交给谁,而且没有权限之分.这种多重绑定便称之为端口复用 ...

  10. EXTJS 4.2 资料 控件之Grid 那些事

    最近在学习Extjs4.2 ,积累文章,看得不错,再此留年: //表格数据最起码有列.数据.转换原始数据这3项 Ext.onReady(function(){ //定义列 var columns = ...