【JAVA、C++】LeetCode 003 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.
解题思路一
由于题目给个tag为HashTable和Two Pointers
因此,我们可以定义一个HashTable,和两个Pointers,index1和index2,首先将String中元素不断添加进去,如果发现重复,则删除重复的元素和它之前的元素,它们在String中的位置分别用index1和index2进行标记,最后找到HashTable曾经的最大规模。这种思路的时间复杂度为O(n)代码如下
public class Solution {
static public int lengthOfLongestSubstring(String s) {
char[] char1 = s.toCharArray();
int longestLength = 0;
int startIndex = 0;
int lastIndex = 0;
HashMap<Character, Integer> hashMap = new HashMap<Character, Integer>();
for (int i = 0; i < char1.length; i++) {
if (hashMap.containsKey(char1[i])) {
lastIndex = hashMap.get(char1[i]);
if (longestLength < (i - startIndex)) {
longestLength = i - startIndex;
}
for (int j = startIndex; j <= lastIndex; j++) {
hashMap.remove(char1[j]);
}
startIndex = lastIndex+1;
}
hashMap.put(char1[i], i);
}
if(longestLength<char1.length-startIndex)
longestLength=char1.length-startIndex;
return longestLength;
}
}
但是,提交之后显示Time Limit Exceeded,看来时间复杂度还是太高,究其原因,在于中间有两个for循环,第二个for循环每进行一次删除都必须遍历一边,因此时间开销必然很大。
思路二:
其实每次添加过后,我们不一定要在HashMap中删除重复的元素,我们可以用一个指针记录需要删除元素的位置,如果出现重复元素,就把pointer置为重复元素最后一次出现的位置+1即可,判断之前最大的substring的长度和本次产生的substring长度的大小。之后将本元素添加进HashMap里面。当然,最后还需要判断下最后一次获得的子串的长度和之前长度的比较。
Java代码如下:
public class Solution {
static public int lengthOfLongestSubstring(String s) {
Map<Character, Integer> hashMap = new HashMap<Character, Integer>();
int length = 0;
int pointer = 0;
for (int i = 0; i < s.length(); i++) {
if (hashMap.containsKey(s.charAt(i))&& hashMap.get(s.charAt(i)) >= pointer) {
length = Math.max(i-pointer, length);
pointer = hashMap.get(s.charAt(i)) + 1;
}
hashMap.put(s.charAt(i), i);
}
return Math.max(s.length() - pointer, length);
}
}
C++代码如下:
#include<vector>
#include<unordered_map>
#include<string>
#include<algorithm>
using namespace std;
class Solution {
public:
int lengthOfLongestSubstring(string s) {
unordered_map<char, int> map;
int res = ;
int pointer = ;
int size = s.length();
for (int i = ; i < size; i++) {
unordered_map < char, int >::iterator iter;
iter = map.find(s[i]);
if (iter != map.end() && map[s[i]] >= pointer) {
res = max(res, i - pointer);
pointer = map[s[i]] + ;
}
if (iter != map.end())
map[s[i]] = i;
else
map.insert(unordered_map<char, int>::value_type(s[i], i));
}
return max(size - pointer,res);
}
};
解题思路三:
由于字母有限,使用STL中map开销太大直接使用数组实现map映射即可,C++实现如下:
#include<string>
#include<algorithm>
using namespace std;
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int res = , point = ;
int prev[];
memset(prev, -, sizeof(prev));
for (int i = ; i < s.length(); i++) {
if (prev[s[i]] >= point)
point = prev[s[i]] + ;
prev[s[i]] = i;
res = max(res, i - point + );
}
return res;
}
};
【JAVA、C++】LeetCode 003 Longest Substring Without Repeating Characters的更多相关文章
- 【JAVA、C++】LeetCode 005 Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- 【JAVA、C++】LeetCode 014 Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. 解题思路: 老实遍历即可, ...
- LeetCode #003# Longest Substring Without Repeating Characters(js描述)
索引 思路1:分治策略 思路2:Brute Force - O(n^3) 思路3:动态规划? O(n^2)版,错解之一:420 ms O(n^2)版,错解之二:388 ms O(n)版,思路转变: 1 ...
- [Leetcode]003. Longest Substring Without Repeating Characters
https://leetcode.com/problems/longest-substring-without-repeating-characters/ public class Solution ...
- C++版- Leetcode 3. Longest Substring Without Repeating Characters解题报告
Leetcode 3. Longest Substring Without Repeating Characters 提交网址: https://leetcode.com/problems/longe ...
- 【LeetCode】003. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- 【LeetCode从零单排】No 3 Longest Substring Without Repeating Characters
题目 Given a string, find the length of the longest substring without repeating characters. For exampl ...
- Java [leetcode 3] Longest Substring Without Repeating Characters
问题描述: Given a string, find the length of the longest substring without repeating characters. For exa ...
- LeetCode之Longest Substring Without Repeating Characters
[题目描述] Given a string, find the length of the longest substring without repeating characters. Exampl ...
随机推荐
- 【Gym 100015B】Ball Painting
题 There are 2N white balls on a table in two rows, making a nice 2-by-N rectangle. Jon has a big pai ...
- Yii2 RBAC 用到的表
Yii2 RBAC用到的四张auth表位于 vendor/yiisoft/yii2/rbac/migration文件夹里面,可以用migration生成 yii migrate --migration ...
- NOI题库--图论 宗教信仰
1526:宗教信仰 总时间限制: 5000ms 内存限制: 65536kB 描述 世界上有许多宗教,你感兴趣的是你学校里的同学信仰多少种宗教. 你的学校有n名学生(0 < n <= 500 ...
- 使用Jayrock开源组件创建参数可为空的接口
经过上一篇文章对Jayrock开源组件的分析,我发现了一个问题,就是在写接口的时候,可以设置某个参数为空,可以不需要进行参数的传递,具体写法如下: 图上的test参数就是可空类型,只需标识为int?类 ...
- debian , ubuntu 截取下拉菜单
普通情况下print键很好用的,但是,截下拉菜单的时候,就怎么按都没反应了...然后换了shutter也不行,只能在静态界面截图.所以...然后就有了下面方法. 如果不是gnome怎么办?这个担心显然 ...
- 使用Jquery+EasyUI 进行框架项目开发案例讲解之四 组织机构管理源码分享
http://www.cnblogs.com/huyong/p/3404647.html 在上三篇文章 <使用Jquery+EasyUI进行框架项目开发案例讲解之一---员工管理源码分享> ...
- android经典实战项目视频教程下载
注:这是一篇转载的文章,原文具体链接地址找不到了,将原文分享如下,希望能对看到的朋友有所帮助! 最近在学习android应用方面的技术,自己在网上搜集了一些实战项目的资料,感觉挺好的,发布出来跟大伙分 ...
- 慎用 Enum.GetHashCode()
公司里遗留下了相当多的 Enum.GetHashCode()来获取枚举值的代码 但是这会产生装箱行为的!!因为Enum是值类型,GetHashCode()是Object的方法,调用GetHashCod ...
- 验证码点击刷新 this.src=this.src+'?'+Math.random()
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- linux下python版webshell后门查杀工具
使用说明: 1.查杀指定路径:python webshell.py 路径 2.按时间查找文件:python webshell.py 路径 “2013-09-28 00:00:00″ # -*- cod ...