LeetCode第[3]题(Java):Longest Substring Without Repeating Characters 标签:Linked List
题目中文:没有重复字符的最长子串
题目难度:Medium
题目内容:
Given a string, find the length of the 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.
翻译:
给定一个字符串,计算得到最长子字符串的长度,而子串内不能重复字符。
例子:
给定“abcabcbb”,答案是“abc”,长度为3。
给定“bbbbb”,答案是“b”,长度为1。
给定“pwwkew”,答案是“wke”,长度为3。注意,答案必须是子字符串(连续的),“pwke”是子序列,而不是子字符串。
思路:应为需要的是子串(连续),所以双重循环就可以遍历每一个子串,再用一个函数来判断这个子串内部是否有重复,没有则更新最新的长度
我的代码:
public int lengthOfLongestSubstring(String s) {
int n = s.length(); // size(),length()这类函数不像变量length,这也是要计算的,当调用一次以上,赋值给一个变量减少复杂度
int ans = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j <= n; j++) {
if (isUnique(s, i, j)) {
ans = Math.max(ans, j - i); // 如果唯一,则更新ans
}
}
}
return ans;
}
public static boolean isUnique(String s, int start, int end) {
Set<Character> hset = new HashSet<Character>();
for (int i = start; i < end; i++) {
hset.add(s.charAt(i));
}
return hset.size() == (end - start) ? true : false; // 如果Set的大小没变说明都是唯一的
}
982 / 983 test cases passed. Status: Time Limit Exceeded
超时了。。。明显这个算法复杂度为O(n3),确实大了点。。。
编码期间问题:
1、程序的边界问题困扰了很久,一开始isUnique方法我是设置从 i 到 j 都判断,j到n-1结束,结果发现这样子需要加很多边界判断条件,例如输入是“C”就一个,那么 j 从下标1开始直接就越界根本不会进入循环体。又或者全是同一个字符“cccccccccc”,isUnique只会一直false,ans也不会更新。偷看了下参考答案,发现第一个和我几乎一样,仔细一看发现它的判唯一范围是从 i 到 j 的前一个字符, j的范围从 i+1 一直到 n,这样一来就总会进入循环体,并且ans至少更新一次。
答案一:
code略
和我的几乎一样就是判断唯一的方法里它是这么写的:
public boolean allUnique(String s, int start, int end) {
Set<Character> set = new HashSet<Character>();
for (int i = start; i < end; i++) {
Character ch = s.charAt(i);
if (set.contains(ch)) return false;
set.add(ch);
}
return true;
}
可能比我的算法复杂度低一点,因为发现重复的时候它可以直接返回,而我那个是全部添加后才能判断重复。记住set、map都有contains功能。
答案二:
public static int lengthOfLongestSubstring2(String s) {
int n = s.length(), ans = 0;
Map<Character, Integer> map = new HashMap<Character, Integer>();
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;
}
983 / 983 test cases passed. Runtime: 66 ms beats 25.38% 算法复杂度:O(N)
思路:利用map的key-value结构存储值以及对应顺位(下标+1),然后每次指针(j)下移的时候都判断是否已经包含了此字符,如果是则将 i 的值更新,所以每次ans都是取最大的 i 到 j (包括j)的长度。
所以而 i 指向的一直是不包含重复的子串开头,而 j 指向就是结尾。ans的每次更新,保证了ans是最大的长度。
例如 abcdaa
当 j = 4时,发现map内有此时 j 指向的a,所以更新 i 的值,表明之前的a在顺位第 1 个,所以ans就等于新a的位置(j)减去老a的位置(顺位 i -1),所以相当于 j - i +1 = 4。
当 j = 5时,发现map内有此时 j 指向的a,所以更新 i 的值,表明之前的a在顺位第 5 个,所以ans_temp = 5-5+1 = 1,取max ,ans最终等于4.
为什么不直接存下标?
如果存储的是下标,那么最后更新的ans也只能是 j - i (新位置减去老位置),假如此时整个字符串内都没有重复,那么最后的答案就是length - 1 - 0 ,因为只有当有更新的时候才能下标直接相减。
答案三:
public int lengthOfLongestSubstring(String s) {
int n = s.length(), ans = 0;
int[] index = new int[256]; // current index of character
// try to extend the range [i, j]
for (int j = 0, i = 0; j < n; j++) {
i = Math.max(index[s.charAt(j)], i);
ans = Math.max(ans, j - i + 1);
index[s.charAt(j)] = j + 1;
}
return ans;
}
983 / 983 test cases passed. Runtime: 66 ms beats 83.43% 算法复杂度:O(N)
其实思想和方法二差不多,有点取巧性质。。。因为字符都能用ascall码表示,而ascall码一共就256个,每一个代表一个字符,(因为后128个键盘输入不了,用128也行)所以直接使用一个int数组当哈希表使用,降低了哈希表的寻址时间复杂度。
以后遇见hash表的key为单个字符时,都可以想到用int[256]来替代map.get(*);
LeetCode第[3]题(Java):Longest Substring Without Repeating Characters 标签:Linked List的更多相关文章
- 【LeetCode每天一题】Longest Substring Without Repeating Characters(最长无重复的字串)
Given a string, find the length of the longest substring without repeating characters. Example 1: ...
- Leetcode第三题《Longest Substring Without Repeating Characters》
题目: Given a string, find the length of the longest substring without repeating characters. For examp ...
- LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters
LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...
- 【LeetCode刷题系列 - 003题】Longest Substring Without Repeating Characters
题目: Given a string, find the length of the longest substring without repeating characters. Example 1 ...
- 【Leetcode】【Medium】Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
- 刷题3. Longest Substring Without Repeating Characters
一.题目 Longest Substring Without Repeating Characters,具体请自行搜索. 这个题目,我看了一下,经过一番思考,我觉得实现起来不是很复杂. 但要做到bug ...
- LeetCode 3 Longest Substring Without Repeating Characters 解题报告
LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...
- leetcode第三题Longest Substring Without Repeating Characters java
Longest Substring Without Repeating Characters Given a string, find the length of the longest substr ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串 C++实现java实现
最长无重复字符的子串 Given a string, find the length of the longest substring without repeating characters. Ex ...
随机推荐
- Visual Assist X助手的一些使用技巧和快捷键
部分快捷键 Shift+Alt+F // Find References 查找引用 Shift+Alt+S // FindSynbolDialog打开查找符号对话框 Alt+G // GotoImpl ...
- 看用Tornado如何自定义实现表单验证
我们知道,平时在登陆某个网站或软件时,网站对于你输入的内容是有要求的,并且会对你输入的错误内容有提示,对于Django这种大而全的web框架,是提供了form表单验证功能,但是对于Tornado而言, ...
- shell正则式解析身份证和手机号
cat test2.html | sed -e 's/\(^\|[^0-9]\)\(13[0-9][0-9]\{8\}\|14[579][0-9]\{8\}\|15[0-3,5-9][0-9]\{8\ ...
- LAMP兄弟连 视频教程集
电驴的资源:http://www.verycd.com/topics/2843130/?ref=msg
- Unity3d依赖于平台的编译
Unity的这一功能被命名为"依赖于平台的编译". 这包括了一些预编译处理指令,让你能够专门的针对不同的平台分开编译和运行一段代码. 此外,你能够在编辑器下运行一些代码用于測试而不 ...
- (2.2)学习笔记之mysql基础操作(登录及账户权限设置)
本系列学习笔记主要讲如下几个方面: 本文笔记[三:mysql登录][四:账户权限设置][五:mysql数据库安全配置] 三.mysql登录 常用登录方式如下: 四.账户权限设置 (4.1)查看用户表, ...
- fopen() r+、w+属性详解
r+具有读写属性,从文件头开始写,保留原文件中没有被覆盖的内容: w+具有读写属性,写的时候如果文件存在,会被清空,从头开始写. r 打开只读文件,该文件必须存在. r+ 打开可读写的文件,该文件必须 ...
- Redis六(管道)
管道 为什么使用管道? Redis是一个TCP服务器,支持请求/响应协议. 在Redis中,请求通过以下步骤完成: 客户端向服务器发送查询,并从套接字读取,通常以阻塞的方式,用于服务器响应. 服务器处 ...
- Python基础-面向对象1
class Bar: def fansik(self, name, age): print(name, age) obj = Bar() print(obj.fansik('fanjinbao', 1 ...
- NoSQL选型及HBase案例详解(转)
从 NOSQL的类型到 常用的产品,我们已经做过很多关于NoSQL的文章,今天我们从国内著名的互联网公司及科研机构的实战谈一下NoSQL数据库. NoSQL一定程度上是基于一个很重要的原理—— CAP ...