【Leetcode】【Medium】Longest Substring Without Repeating Characters
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的更多相关文章
- LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters
LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...
- (python)leetcode刷题笔记03 Longest Substring Without Repeating Characters
3. Longest Substring Without Repeating Characters Given a string, find the length of the longest sub ...
- Leetcode第三题《Longest Substring Without Repeating Characters》
题目: Given a string, find the length of the longest substring without repeating characters. For examp ...
- LeetCode 3 Longest Substring Without Repeating Characters 解题报告
LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...
- 【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 ...
- 【LeetCode】Longest Substring Without Repeating Characters 解题报告
[题意] Given a string, find the length of the longest substring without repeating characters. For exam ...
- 【LeetCode】3 、Longest Substring Without Repeating Characters
题目等级:Medium 题目描述: Given a string, find the length of the longest substring without repeating chara ...
- 【一天一道LeetCode】 #3 Longest Substring Without Repeating Characters
一天一道LeetCode (一)题目 Given a string, find the length of the longest substring without repeating charac ...
- 【LeetCode OJ】Longest Substring Without Repeating Characters
题目链接:https://leetcode.com/problems/longest-substring-without-repeating-characters/ 题目:Given a string ...
随机推荐
- 百度ECharts数据绑定诀窍
百度Echarts的功能还是蛮好用的.. 不能说多好但是也不次.. 下边就分享一些数据绑定经验..对在处理过程中的思路有一些帮助... 报表里用的最多的可以说是 饼状图和柱形图.. 饼状图里当然是包括 ...
- Delphi对话框初始地址InitialDir
我的电脑:SaveDialog1.InitialDir := '::{20D04FE0-3AEA-1069-A2D8-08002B30309D}';// My Computer {20D04FE0-3 ...
- 已有插件支持requirejs
define(["jquery"], // Require jquery function($){ //把你原来的插件代码放这里吧,这样就行了 //注意文件命名 }) ...
- Mybatis多参数
转载自:一杯甜酒 http://blog.csdn.net/u012562943/article/details/52316071 据我目前接触到的传多个参数的方案有三种.第一种方案 DAO层的函数方 ...
- Java入门系列-26-JDBC
认识 JDBC JDBC (Java DataBase Connectivity) 是 Java 数据库连接技术的简称,用于连接常用数据库. Sun 公司提供了 JDBC API ,供程序员调用接口和 ...
- java.lang.RuntimeException Unable to instantiate application Caused by: java
参考:http://blog.csdn.net/xufazhong/article/details/71155528 dependencies { classpath 'com.android.too ...
- Exists 和 Not Exists
只注重子查询是否有返回行,如有返回结果为真,否则为假,并不适用子查询的结果,仅用于测试子查询是否有返回结果. 语法: if exists (子查询) begin 语句块 end 例子: if exis ...
- 007.ASP.NET MVC控制器依赖注入
原文链接:http://www.codeproject.com/Articles/560798/ASP-NET-MVC-Controller-Dependency-Injection-for-Be 前 ...
- 流畅的python和cookbook学习笔记(九)
1.减少可调用对象的参数个数,使用functools.partial冻结参数 使用functools.partial(),可以固定一个或者多个值,减少调用参数. >>> def sp ...
- https加解密过程
前前后后,看了许多次关于https加解密过程的相关文档资料,一直似懂非懂.这次,终于理解了,还画了个图,做个记录. 知识点 1.对称加密:双方用同一个密码加解密.如des,aes 2.非对称加密:双方 ...