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.

解题思路1,o(n):

新建一个字符串变量sub用来保存当前处理的子字符串,从起始遍历原字符串S中的每个字符;

对于每一个遍历到的字符S[i]:

  如果S[i]已存在sub中(例sub[j] = S[i]),则sub切掉sub[j]及其之前的所有字符,并在末尾插入S[i];

  如果S[i]不存在sub中,则在sub末尾插入S[i]。

  遍历过程中,随时记录sub曾经达到的最大长度maxlength

遍历结束,返回maxlength;

代码:

 class Solution {
public:
int lengthOfLongestSubstring(string s) {
int string_length = s.size();
if (string_length <= )
return string_length; string substring = string(, s[]);
int current_length = ;
int max_length = ; for (int i = ; i < string_length; ++i) { //current_length + string_length - i > longest_length
int pos = substring.find(s[i]);
if (pos != -) {
substring = substring.substr(pos + ) + s[i];
current_length -= pos;
} else {
substring += s[i];
current_length++;
if (current_length > max_length)
max_length = current_length;
}
} return max_length;
}
};

解题思路2,o(n):

用一个128个元素的int型数组postions,记录字符串中的每个字符,在字符串中上次出现的位置(之前从未出现则为-1);同时用一个int变量ind记录子串的开始位置;

当遍历到字符串某一个元素S[i]时:

  查看postion数组中记录的此字符上次出现的位置positions[S[i]],

  如果positions[S[i]]大于ind,说明S[i]在当前子串中有重复,则ind = positions[S[i]] + 1;

  如果positions[S[i]]小于ind,说明当前子串中,不包含S[i],则ind不变;

  随时记录子串的最大长度(i - ind + 1)

循环结束,返回最大长度值。

代码:

 class Solution {
public:
int lengthOfLongestSubstring(string s) {
int positions[];
memset(positions, -, sizeof(positions)); int ind = ;
int max = ;
for (int i = ; i < s.size(); i++){
if (positions[s[i]] >= ind)
ind = positions[s[i]] + ; if (i - ind + > max)
max = i - ind + ; positions[s[i]] = i;
} return max;
}
};

附录:

C++ string操作

【Leetcode】【Medium】Longest Substring Without Repeating Characters的更多相关文章

  1. LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters

    LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...

  2. (python)leetcode刷题笔记03 Longest Substring Without Repeating Characters

    3. Longest Substring Without Repeating Characters Given a string, find the length of the longest sub ...

  3. Leetcode第三题《Longest Substring Without Repeating Characters》

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

  4. LeetCode 3 Longest Substring Without Repeating Characters 解题报告

    LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...

  5. 【LeetCode算法题库】Day1:TwoSums & Add Two Numbers & Longest Substring Without Repeating Characters

    [Q1] Given an array of integers, return indices of the two numbers such that they add up to a specif ...

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

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

  7. 【LeetCode】3 、Longest Substring Without Repeating Characters

    题目等级:Medium 题目描述:   Given a string, find the length of the longest substring without repeating chara ...

  8. 【一天一道LeetCode】 #3 Longest Substring Without Repeating Characters

    一天一道LeetCode (一)题目 Given a string, find the length of the longest substring without repeating charac ...

  9. 【LeetCode OJ】Longest Substring Without Repeating Characters

    题目链接:https://leetcode.com/problems/longest-substring-without-repeating-characters/ 题目:Given a string ...

随机推荐

  1. 同时安装Python2和Python3,如何兼容并切换使用详解

    由于历史原因,Python有两个大的版本分支,Python2和Python3,又由于一些库只支持某个版本分支,所以需要在电脑上同时安装Python2和Python3,因此如何让两个版本的Python兼 ...

  2. Python与C相互调用、编译

    因为最近学习Boost::python的缘故,想尝试下不同语言之间的相互编译. 参考资料:http://blog.csdn.net/joliny/article/details/2457197. 很吃 ...

  3. TSM_ISSUE_123

    dsmc 命令详解http://www-ik.fzk.de/~apel/html/adsm_manual.htmlTSM InfoCenterhttp://www-01.ibm.com/support ...

  4. 解决chrome浏览器对于自动填充的input表单添加的默认的淡黄色背景问题 && 一般的浏览器input和button的高度不一致问题

    解决chrome浏览器对于自动填充的input表单添加的默认的淡黄色背景问题 如果我们把一个表单设置位 autofocus ,这时这个表单在获取焦点后就会产生淡黄色的背景,我们就是使用!importa ...

  5. Cloudera Manager安装之Cloudera Manager 5.6.X安装(tar方式、rpm方式和yum方式) (Ubuntu14.04) (三)

    见 Ubuntu14.04下完美安装cloudermanage多种方式(图文详解)(博主推荐) 欢迎大家,加入我的微信公众号:大数据躺过的坑     免费给分享       同时,大家可以关注我的个人 ...

  6. 网络安装Ubuntu16.04

    网络安装Ubuntu16.04 搭建PXE服务器 PXE是Pre-boot Execution Environment,预启动执行环境.是通过网络安装任何linux系统最重要的步骤. 首选搭建PXE服 ...

  7. C#中加粗label的字体

    1. 在C#的代码中想直接加粗label控件的字体, label1.Font.Bold = true;//发现系统会提示Font.Bold是只读属性 如果必须要加粗字体呢,方法如下: 2. 使用Fon ...

  8. Call to a member function assign() on null

    Thinkphp: 在子控制器里面写了一个构造函数,如下 //构造函数 public function __construct(){ echo 1; } 结果页面报错了  ---->  Call ...

  9. 【c++】输出文件的每个单词、行

    假设文件内容为 1. hello1 hello2 hello3 hello4 2. dsfjdosi 3. skfskj ksdfls 输出每个单词 代码 #include <iostream& ...

  10. bzoj 5315: [Jsoi2018]防御网络

    Description Solution 考虑每一条边的贡献 对于树边,如果两边各存在一个点,那么有贡献,总贡献就是 \((2^{size}-1)*(2^{n-size}-1)\) 分别对应两边的 \ ...