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. 前端+php实现概率抽奖

    转前端之后,后台工程师大大跑路了只能兼任他的位置写点东西了 前端+后台抽奖代码网上一大堆,引用一位仁兄前面的代码(比较懒抱歉,后面数据处理,奖项判断是否抽完我将会标红,因为前面的代码网上太多了都能找到 ...

  2. Unix_JDK安装及配置

    CentOS 下过程 JDK 在 CentOS 和 Ubuntu 下安装过程是一样的,所以这里不再讲 Ubuntu 系统下的安装 JDK 1.8 下载 此时(20170906)最新版本:jdk-8u1 ...

  3. linux磁盘分区fdisk命令详解

    1.什么是分区?    分区是将一个硬盘驱动器分成若干个逻辑驱动器,分区是把硬盘连续的区块当做一个独立的磁硬使用.分区表是一个硬盘分区的索引,分区的信息都会写进分区表.2.为什么要有多个分区? 防止数 ...

  4. Eclipse for J2EE+tomcat配置上的一些问题

    之前下载了一个eclipse基础版本,学习之前没有什么感觉不方便的地方,方式学习到web编程时,就出现问题了,因为不支持创建web项目. 于是下载了一个Eclipse for J2EE版本的工具,但是 ...

  5. orcale 之PL/SQL 控制语句

    控制语句是PL/SQL 的关键所在.只有学好这些控制语句才能在工作中更好的实现各种的功能. 选择结构 1. IF 语句 和其他的编程语言很类似.它的具体机构如下: IF(条件)THEN {语句} EL ...

  6. Linux 命令 -- chmod

    chmod命令用来变更文件或目录的权限.在UNIX系统家族里,文件或目录权限的控制分别以读取.写入.执行3种一般权限来区分,另有3种特殊权限可供运用.用户可以使用chmod指令去变更文件与目录的权限, ...

  7. Java中break、continue及标签等跳转语句的使用[下]

    作为上一篇使用for循环演示的跳转,这一篇将使用while.相比较来说,while比for循环更简单.代码如下: public class LabeledWhile { public static v ...

  8. [转]微信小程序-template模板使用

    本文转自:http://blog.csdn.net/u013778905/article/details/59646241 如下图,我在做华企商学院小程序的时候,课程搜索结果页和课程列表页结构是完全一 ...

  9. WPF 资源字典

    使用好处:存储需要被本地话的内容(错误消息字符串等,实现软编码),减少重复的代码,重用样式,实现多个项目之间的共享资源;修改一个地方所有引用的地方都会被修改,方便统一风格; 使用方法,归纳起来主要有下 ...

  10. JMS消息中间件之ActiveMQ学习

    1.下载 下载二进制bin文件:http://activemq.apache.org/activemq-5132-release.html 下载源码: 2.启动: 解压任意路径: 启动后: 3.访问: ...