算法练习3.Longest Substring Without Repeating Characters 最长不重复子字符串(map)
2016-04-30 09:30
375 查看
3. Longest Substring Without Repeating Characters
https://leetcode.com/problems/longest-substring-without-repeating-characters/
Examples:
Given "abcabcbb"
,
the answer is "abc"
,which
the length is 3.
Given "bbbbb"
,
the answer is "b"
,with
the length of 1.
Given "pwwkew"
,
the answer is "wke"
,with
the length of 3. Note that the answer must be a substring, "pwke"
is
a subsequence and not a substring.
最长不重复子字符串#include
<map>
#include<string>
using namespace std;
class
Solution {
public:
int lengthOfLongestSubstring(strings) {
map<char,int>map1;
map<char,int>::iterator it;
int sum = 0, max = 0,index = 0;;
for (int i = 0; i <s.length(); i++)
{
if (map1.count(s.at(i)) == 0)
{
map1[s.at(i)] = i;
sum++;
if (sum>max)
{
max = sum;
}
}
else{
sum = i - map1[s.at(i)];
index = map1[s.at(i)];
it = map1.begin();
while( it != map1.end())
{
if (it->second<= index)
{
it = map1.erase(it);
}
else{
++it;
}
}
map1[s.at(i)]=i;
}
}
return max;
}
};
以下内容转自LeetCode官网:
Approach #1 Brute Force [Time LimitExceeded]
Java
publicclassSolution{
publicintlengthOfLongestSubstring(String
s) {
int n= s.length();
int ans=0;
for (int i=0;
i< n; ++i)
for (int j= i+1;
j<= n; ++j)
if (allUnique(s,i, j)) ans= Math.max(ans,
j- i);
return ans;
}
publicbooleanallUnique(String s,int
start,int end) {
Set<Character> set= new HashSet<>();
for (int i=start; i< end; ++i)
{
Character ch = s.charAt(start);
if (set.contains(ch)) return false;
set.add(ch);
}
return true;
}
}
ComplexityAnalysis
Time complexity : O(n^3)O(n3).
Space complexity : O(min(n, m))O(min(n,m)). We need O(k)O(k) space for checking a substring has no duplicate characters, where kk is the size of the Set.
The size of the Set is upper bounded by the size of the string nn and the size of the charset/alphabet mm.
Approach #2 Sliding Window [Accepted]
JavapublicclassSolution{
publicintlengthOfLongestSubstring(String
s) {
int n= s.length();
Set<Character> set= new HashSet<>();
int ans=0,
i=0, j=0;
while (i
< n && j < n){
// try to extendthe range [i, j]
if (!set.contains(s.charAt(j))){
set.add(s.charAt(j++));
ans = Math.max(ans, j-
i);
}
else {
set.remove(s.charAt(i++));
}
}
return ans;
}
}
Complexity Analysis
· Time complexity : O(2n) = O(n)O(2n)=O(n).
In the worst case each character willbe visited twice by ii and jj.
· Space complexity : O(min(m, n))O(min(m,n)).
Same as the previous approach. Weneed O(k)O(k) space
forthe sliding window, where kk is the size of the
Set.
The size of the Set is upper bounded by the size of the string nn and thesize of the charset/alphabet mm.
Approach #3 Sliding Window Optimized[Accepted]
Java (Using HashMap)public class Solution {
public intlengthOfLongestSubstring(String s) {
int n = s.length(), ans = 0;
Map<Character, Integer> map = new HashMap<>(); // current index of character
// try to extend the range [i, j]
for (int j = 0, i = 0; j < n; ++j) {
if (map.containsKey(s.charAt(j))) {
i = Math.max(map.get(s.charAt(j)), i);
}
ans = Math.max(ans, j - i + 1);
map.put(s.charAt(j), j + 1);
}
return ans;
}
}
Time complexity : O(n)O(n). Index jj will iterate nn times.
Space complexity (HashMap) : O(min(m, n))O(min(m,n)). Same as the previous approach.
修改:省去删除keys的步骤,滑动窗口方式,重复key在窗口左边界左边的取窗口左边界,判断窗口大小与当前最大值,覆盖更新重复key;
#include
<map>
#include<string>
using namespace std;
class
Solution {
public:
int lengthOfLongestSubstring(strings) {
map<char,int>map1;
map<char,int>::iterator it;
int sum = 0, max =0,j=0;
for (int i = 0; i <s.length(); i++)
{
if (map1.count(s.at(i)) != 0)
{
if (map1[s.at(i)]>j)
{
j = map1[s.at(i)];
}
}
if (i - j +1>sum) //注意!!!
{
sum = i - j+1;
}
map1[s.at(i)] = i+1; //注意是i+1!!!
}
return sum;
}
};
相关文章推荐
- 个人冲刺10
- redis基础
- 宏定义中的"#"、"##"、"..."、"__VA_AGRS__"
- 1002. 写出这个数 (20)
- 第 15 章 CSS 文本样式[下]
- bzoj4310: 跳蚤
- POJ1200 Crazy Search(hash)
- 博客莫名被关,还好及时解决
- ADT之练习题
- 创业分享:创业的过程就是坚持的过程
- tomcat环境变量配置
- 广告行业知识
- phoenix设计
- iOS开发笔记--Runtime那点事
- List--ArrayList(浅识)一
- Xamarin 手动安装
- 1001. 害死人不偿命的(3n+1)猜想 (15)
- 《wonderful tonight》
- 表格的相关操作
- hive 建立表结构 ,添加分区