【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 ...
随机推荐
- Log4Net使用详解
1.Log4Net环境的搭建与基本配置 (1)Log4Net框架介绍 Log4net 是 Apache 下一个开放源码的项目,它是Log4j 的一个克隆版.我们可以控制日志信息的输出目的地.L ...
- ztree点击文字勾选checkbox,radio实现方法
ztree的复选框checkbok,单选框radio是用背景图片来模拟的,所以点击文字即使用label括起checkbox,radio文字一起,点击文字也是无法勾选checkbox. 要想点击ztre ...
- js弹出窗口总结6种弹窗方法
注: //关闭,父窗口弹出对话框,子窗口直接关闭 this.Response.Write("<script language=javascript>window.close(); ...
- linux 问答
问:1 如何查看当前的Linux服务器的运行级别? 答: ‘who -r’ 和 ‘runlevel’ 命令可以用来查看当前的Linux服务器的运行级别. 问:2 如何查看Linux的默认网关? 答: ...
- 16.(转) Android之Support v4、v7、v13的区别和应用场景
我们在项目中经常会碰到Android Support v4.v7和v13包兼容问题,所以有必要梳理下这些东西. google提供了Android Support Library package 系列的 ...
- poj 3734 矩阵快速幂+YY
题目原意:N个方块排成一列,每个方块可涂成红.蓝.绿.黄.问红方块和绿方块都是偶数的方案的个数. sol:找规律列递推式+矩阵快速幂 设已经染完了i个方块将要染第i+1个方块. a[i]=1-i方块中 ...
- BZOJ 1066 POJ 2711 [SCOI2007]蜥蜴
与POJ 1815 Friendship类似,该题之前也做过 目前处于TLE状态.样例已经通过 1066: [SCOI2007]蜥蜴 Time Limit: 1 Sec Memory Limit: ...
- C语言学习-01第一个C语言程序
一 C语言的历史 C语言是一门通用计算机编程语言,应用广泛.C语言的设计目标是提供一种能以简易的方式编译.处理低级存储器.产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言. 尽管C语言提供 ...
- FITTING A MODEL VIA CLOSED-FORM EQUATIONS VS. GRADIENT DESCENT VS STOCHASTIC GRADIENT DESCENT VS MINI-BATCH LEARNING. WHAT IS THE DIFFERENCE?
FITTING A MODEL VIA CLOSED-FORM EQUATIONS VS. GRADIENT DESCENT VS STOCHASTIC GRADIENT DESCENT VS MIN ...
- 看过《大湿教我写.net通用权限框架(1)之菜单导航篇》之后发生的事(续)——主界面
引言 在UML系列学习中的小插曲:看过<大湿教我写.net通用权限框架(1)之菜单导航篇>之后发生的事 在上篇中只拿登录界面练练手,不把主界面抠出来,实在难受,严重的强迫症啊.之前一直在总 ...