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. Git远程管理[五]

    标签(linux): git 笔者Q:972581034 交流群:605799367.有任何疑问可与笔者或加群交流 相关命令 git clone https://github.com/guohongz ...

  2. 监控 Linux 性能的 18 个命令行工具[转]

    对于系统和网络管理员来说每天监控和调试Linux系统的性能问题是一项繁重的工作.在IT领域作为一名Linux系统的管理员工作5年后,我逐渐认识到监控和保持系统启动并运行是多么的不容易.基于此原因,我们 ...

  3. handlebars.js模版引擎随记

    前台的模版引擎有许多种,相比较而言 我个人更觉得handlebars更为轻便 首先github上下载自新版本的handelbars.js http://handlebarsjs.com 下载下来之后呢 ...

  4. python中的字符串格式化

    Python中常见的字符串格式化方式包括两种:字符串插入(str%),format函数(str.format()) 1.字符串插入 字符串插入是设置字符串格式的简单方法,与C语言.Fortran语言差 ...

  5. bzoj 4872: [Shoi2017]分手是祝愿 [期望DP]

    4872: [Shoi2017]分手是祝愿 题意:n个灯开关游戏,按i后i的约数都改变状态.随机选择一个灯,如果当前最优策略\(\le k\)直接用最优策略.问期望步数\(\cdot n! \mod ...

  6. ajax请求 readyState为0 可能原因之一

    问题:同样的代码逻辑,PC端和iOS都能正常访问,但是Android系统请求都是报错: 上网查阅,关于ajax请求失败且状态码都是0的情况有很多,最后排查的原因是:域名证书问题:

  7. ssh快速登录远程服务器

    以前我的做法是创建一个shell脚本运行,今天了解了可以按照下面的方式: vi .ssh/config ,写入如下内容 HOST api User abc Port HostName 123.123. ...

  8. PHPUnit-附录 C. XML 配置文件

    [http://www.phpunit.cn/manual/5.7/zh_cn/appendixes.configuration.html] PHPUnit <phpunit> 元素的属性 ...

  9. python学习:匿名函数

    Python 函数 lambda   匿名函数 -lambda 函数是一种快速定义单行的最小函数,可以用在任何需要函数的地方.   def fun(x,y):        return x*y fu ...

  10. 一步一步从原理跟我学邮件收取及发送 13.mime格式与常见字符编码

    在前面的本系列文章中我们已经学会了邮件的发送和收取.但在收取中我们看到的是一串串的乱码,回忆前面的发送过程,我们会奇怪:我们前面的邮件是明文啊.为什么明文的邮件明明也可以正常工作,还要弄乱码似的字符串 ...