【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 ...
随机推荐
- jsp、Servlet的面试题
3. 谈谈Servlet的生命周期 当接收到请求的时候,容器察看对应的Servlet对象是否存在,如果不存在,需要加载Servetl,实例化Servlet,调用init方法进行初始化.如果已经存在,根 ...
- 08-oracle统计函数(单组分组函数)
--count时尽量count(列名),count(*)也可以. --count,max,min,sum,avg,median(中位数) select count(empno),count(disti ...
- 源码安装caffe2时遇到的问题解决办法
https://github.com/facebookresearch/DensePose/issues/119
- Robot Framework自动化测试四(分层思想)
谈到Robot Framework 分层的思想,就不得不提“关键字驱动”. 关键字驱动: 通过调用的关键字不同,从而引起测试结果的不同. 在上一节的selenium API 中所介绍的方法其实就是关 ...
- unity状态机实现
刚看了浅墨大神的文章让我对状态机有了进一步的理解 具体实现见装载的状态机文章 首先得有个总状态HeroineBaseState接口,其里面的方法主要是与行为相关的方法,让继承此接口的类来实现的 具体的 ...
- Django级联删除的选项
Django级联删除的选项 Django模型中的on_delete属性具有如下选项: CASCADE 级联删除,也就是被引用的实体被删除后,相关的记录信息都会被删除. PROTECT 阻止删除被引用的 ...
- Struts2拦截器和标签
一.struts2拦截器 1.struts2是框架,封装了很多的功能,struts2里面封装的功能都是在拦截器里面. 2 struts2里面封装了很多的功能,有很多拦截器,不是每次这些拦截器都执行,每 ...
- React.js 小书 Lesson19 - 挂载阶段的组件生命周期(二)
作者:胡子大哈 原文链接:http://huziketang.com/books/react/lesson19 转载请注明出处,保留原文链接和作者信息. 这一节我们来讨论一下对于一个组件来说,cons ...
- WPF简单的数据库查询
做一个简单WPF连接数据库的 控件类型和名称:DataGrid:dataGrid Button1 :Button1 Button: Button2 ...
- [android] 练习使用ListView(二)
主要练习异步任务和LruCache缓存 package com.android.test; import java.io.InputStream; import java.net.HttpURLCon ...