C++解法一:

class Solution {
public:
int lengthOfLongestSubstring(string s) {
int m[] = {},left = , res = ;
for(int index = ; index < s.size(); ++index)
{
if(m[s[index]] == || m[s[index]] < left)
{
res = max(res,index-left+);
}else{
left = m[s[index]];
}
m[s[index]] = index+;
}
return res;
}
};

算法解读:
1) 这里使用哈希的思想,把256个可能的字符都考虑在内,字符的ASCⅡ编码作为数组下标进行映射。如m[a]等价于m[97]。
2) res其实是result的缩写,表示返回的最大无重复子串的长度,left表示当前正在判断的子串的起始位置。
3) 进行一个for循环,当满足(m[s[i]] == 0 || m[s[i]] < left)条件时候,计算子串长度,并且与res比较最长并取之。
  这里的m[s[i]]表示第i个字符上一次出现的位置,如果从来没有出现过即m[s[i]]==0, 则表示无重复;如果m[s[i]] < left
  则表示上次出现的位置在当前判断的字串起始位置的左边,不重复。
  否则,(m[s[i]] != 0 && m[s[i]] >= left),重复。此时要更新left的值为m[s[i]],即不left更新为当前字符与上一次重复
  的字符位置的下一位 。这里的i+1 是因为i是从0开始的下标,而我们需要的是从1开始的序号。
4) i偏移都每一个字符都要几下当前的位置,即m[s[i]] = i + 1 。

C++解法二

复制代码
class Solution {
public:
int lengthOfLongestSubstring(string s) {
vector<int> m(, -);
int res = , left = -;
for (int i = ; i < s.size(); ++i) {
left = max(left, m[s[i]]);
m[s[i]] = i;
res = max(res, i - left);
}
return res;
}
};

算法分析:

1)与算法一思想一样,只是进行了精简。

2)每次循环,将left更新 max(left, m[s[i]]),如果s[i]在left的右边且

3_Longest Substring Without Repeating Characters -- LeetCode的更多相关文章

  1. LeetCode 3_Longest Substring Without Repeating Characters

    LeetCode 3_Longest Substring Without Repeating Characters 题目描写叙述: Given a string, find the length of ...

  2. 3-Longest Substring Without Repeating Characters @LeetCode

    3-Longest Substring Without Repeating Characters @LeetCode 题目 题目中得到的信息有: 一段字符串找出不重复子串的最大长度,只需要长度信息. ...

  3. LeetCode: 3_Longest Substring Without Repeating Characters | 求没有重复字符的最长子串的长度 | Medium

    题目: Given a . For . 解题思路: 这个题让找一个字符串中具有不重复单词的最长子串的长度,如:ababc,子串为abc,长度为3.有这么几个方法: 方法一: 依赖字符串本身的一些特有函 ...

  4. Longest Substring Without Repeating Characters leetcode java

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

  5. Longest Substring Without Repeating Characters ---- LeetCode 003

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

  6. Longest Substring Without Repeating Characters -- LeetCode

    原题链接: http://oj.leetcode.com/problems/longest-substring-without-repeating-characters/ 这道题用的方法是在LeetC ...

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

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

  8. leetcode: longest substring without repeating characters

    July 16, 2015 Problem statement: Longest Substring Without Repeating Characters Read the blog: http: ...

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

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

随机推荐

  1. Unity DoTween 动画使用案例

    这边我就直接放一个标准的Dotween动画的使用demo吧. 这个案例满足应该可以完成你所想实现的几乎所有复杂动画. void PlayTween() { //set tween data float ...

  2. msyql多软件连接

    菜鸟一枚 使用mysql删除数据库时,习惯性使用navicat,由于使用的是sqlyog备份的,就用sqlyog恢复数据库,结果总是报错. 用同事电脑没有问题,后来发现是用了不同的软件,导致数据库数据 ...

  3. 【转】matlab针对不同格式文件的读写

    本技术支持指南主要处理:ASCII, binary, and MAT files.要得到MATLAB中可用来读写各种文件格式的完全函数列表,可以键入以下命令: help iofun MATLAB中有两 ...

  4. tf.train.shuffle_batch函数解析

    tf.train.shuffle_batch (tensor_list, batch_size, capacity, min_after_dequeue, num_threads=1, seed=No ...

  5. pytho字符串处理内置方法一览表

    序号 方法及描述 1 capitalize()将字符串的第一个字符转换为大写 2 center(width, fillchar) 返回一个指定的宽度 width 居中的字符串,fillchar 为填充 ...

  6. NIO基础篇(三)

    NIO里对性能提升最显著的是内存映射(memory mapping),内存访问的速度往往比文件访问的速度快几个数量级. 在内存映射之前,还需要看NIO的一些其他的特性. 缓冲区分片 slice()方法 ...

  7. MySQL死锁[转]

    案例描述       在定时脚本运行过程中,发现当备份表格的sql语句与删除该表部分数据的sql语句同时运行时,mysql会检测出死锁,并打印出日志. 两个sql语句如下:       (1)inse ...

  8. BZOJ 2303: [Apio2011]方格染色 [并查集 数学!]

    题意: $n*m:n,m \le 10^6$的网格,每个$2 \times 2$的方格必须有1个或3个涂成红色,其余涂成蓝色 有一些方格已经有颜色 求方案数 太神了!!!花我三节课 首先想了一下只有两 ...

  9. POJ 2404 Jogging Trails [DP 状压 一般图最小权完美匹配]

    传送门 题意:找一个经过所有边权值最小的回路,$n \le 15$ 所有点度数为偶则存在欧拉回路,直接输出权值和 否则考虑度数为奇的点,连着奇数条边,奇点之间走已经走过的路移动再走没走过的路 然后大体 ...

  10. HttpGet HttpPost

    public string HttpGet(string Url, string postDataStr) { HttpWebRequest request = (HttpWebRequest)Web ...