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. Access和Sql区别

    假设表game有一字段为gameYuiJian为bit字段(SQL SERVER 20005)和"是/否"字段(ACCSS数据库),在编写脚本文件时,如下才能正确执行 SQL st ...

  2. UITableView的简单使用

    UITableView分为两种style:UITableViewStyleGrouped和UITableViewStylePlain. (一)UITableViewStyleGrouped #impo ...

  3. @override

    目录 用处 作用 注意   用处: 继承抽象类,必须实现抽象方法,方法上要加@override 实现接口时,必须实现接口里定义的方法,方法上要加@override         作用: 可以检查方法 ...

  4. 找个输入IPoint在某个FeatureClass上距离最近的要素

      /// <summary> /// 得到输入点在输入图层上的最近点 /// </summary> /// <param name="randomPoints ...

  5. Windows VC++常见问题汇总

    1.warning C4996: 'setmode': The POSIX name for this item is deprecated. Instead, use the ISO C++ con ...

  6. C/C++笔试题目

    1. C语言中无符号数与有符号数 unsigned ; ; printf( printf( ? 有符号数和无符号数在进行比较运算时(==,>=,<=,>,<),有符号数隐式的转 ...

  7. coreseek安装使用

    本文引自:http://www.phperz.com/article/14/0615/95.html sphinx的安装使用,其实都是大同小异,以下以 coreseek安装为例Coreseek 是一款 ...

  8. DTCMS栏目首页,循环输出子栏目和新闻

    <%set DataTable categoryList=get_category_child_list(channel,0)%> <%foreach(DataRow cdr in ...

  9. [转]SET NOCOUNT ON

    ref: http://www.cnblogs.com/jayleke/archive/2010/07/10/1774758.html 在存储过程,触发器中,经常用到SET NOCOUNT ON: 作 ...

  10. 转几篇WPF文章

    How to view word document in WPF application (CSVSTOViewWordInWPF) WPF 浏览PDF 文件 如何保存RichTextBox的文本到数 ...