LeeCode(No3 - Longest Substring Without Repeating Characters)
题目:
Given a string, find the length of the longest substring without repeating characters.
示例: 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.
问题的难点在于光标移动过程中,一旦遇到重复需要重新开始计算长度
第一次提交的代码如下:
class Solution {
public int lengthOfLongestSubstring(String s) {
int longestLength = 0;
int currentLength = 0;
String longestStr = "";
char tmpChar;
int repeatIndex = 0; for(int i = 0; i < s.length(); i++){
tmpChar = s.charAt(i);
repeatIndex = longestStr.indexOf(tmpChar, 0);
// 遇到重复,从重复的下一位开始
if(repeatIndex >= 0) {
if(longestLength < currentLength){
longestLength = currentLength;
}
longestStr = longestStr.substring(repeatIndex + 1) + tmpChar;
currentLength = longestStr.length();
} else {
longestStr += s.charAt(i);
currentLength++;
}
} if(longestLength < currentLength) {
return currentLength;
} return longestLength;
}
}
提交后执行效率只击败了15.65%
为提高效率,考虑更新如下方面:
1. String 用StringBuilder替换(不需要线程安全,所以不使用开销大的StringBuffer)
2. char用String替换(是为了避免循环中char到String转换时的开销)
修改后的代码如下:
class Solution {
public int lengthOfLongestSubstring(String s) {
int longestLength = 0;
int currentLength = 0;
StringBuilder builder = new StringBuilder();
String charStr;
int repeatIndex; for(int i = 0; i < s.length(); i++){
charStr = s.substring(i, i + 1);
repeatIndex = builder.indexOf(charStr, 0);
// 遇到重复,从重复的下一位开始
if(repeatIndex >= 0) {
if(longestLength < currentLength){
longestLength = currentLength;
}
builder.delete(0, repeatIndex + 1);
builder.append(charStr);
currentLength = builder.length();
} else {
builder.append(charStr);
currentLength++;
}
} if(longestLength < currentLength) {
return currentLength;
} return longestLength;
}
}
提交后执行效率只击败了65.57%
如何再进一步提高效率呢?因为循环里面有查询字符存在的功能,考虑替换数据结构,用HashMap替换StringBuffer,
但是在思考了一会没有找到使用HashMap的写法,于是直接参考了现有的方案,如下:
public class Solution {
public int lengthOfLongestSubstring(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;
}
}
其实实现的逻辑跟之前的思路一致,j 作为整个字符串的额游标,i 作为遇到重复元素后重新开始位置的游标,ans记录最大值
这样的写法不但是用HashMap来代替String查找,而且不用单独存储子字符串。
复杂度如下:
Complexity Analysis
Time complexity : O(n)O(n). Index jj will iterate nn times.
Space complexity (HashMap) : O(min(m, n)).
参考:
https://leetcode.com/problems/longest-substring-without-repeating-characters
LeeCode(No3 - Longest Substring Without Repeating Characters)的更多相关文章
- 最长子串(Leetcode-3 Longest Substring Without Repeating Characters)
Question: Given a string, find the length of the longest substring without repeating characters. Exa ...
- LeeCode Algorithm #3 Longest Substring Without Repeating Characters
一开始以为是不连续的,其实要求子串是连续的.想法:two-pointer O(n)时间,再借助256大小的int数组.两个下标i,j(i<=j).对于i=0,找到最右侧的字符不重复的下标的后一位 ...
- LeetCode 3. 无重复字符的最长子串(Longest Substring Without Repeating Characters)
题目描述 给定一个字符串,找出不含有重复字符的最长子串的长度. 示例: 给定 "abcabcbb" ,没有重复字符的最长子串是 "abc" ,那么长度就是3. ...
- LeetCode:Longest Substring Without Repeating Characters(最长不重复子串)
题目链接 Given a string, find the length of the longest substring without repeating characters. For exam ...
- Longest Substring Without Repeating Characters(Difficulty: Medium)
题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...
- LeetCode 3 Longest Substring Without Repeating Characters(最长不重复子序列)
题目来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, f ...
- 【leetcode】Longest Substring Without Repeating Characters (middle)
Given a string, find the length of the longest substring without repeating characters. For example, ...
- 3. Longest Substring Without Repeating Characters (ASCII码128个,建立哈西表)
Given a string, find the length of the longest substring without repeating characters. For example, ...
- LeetCode OJ:Longest Substring Without Repeating Characters(最长无重复字符子串)
Given a string, find the length of the longest substring without repeating characters. For example, ...
随机推荐
- re.I re.L re.M re.S re.U re.X
- loader的简单使用过程分析
首先,fragment或者activity必须实现callback接口 必须实现的三个方法为 public Loader<Cursor> onCreateLoader(int id, Bu ...
- 关于android中出现failed to read row 0,column -1错误
该错误出现的原因是Cursor.getColumnIndex()的参数列名不存在或者错误,这时返回值为-1.出现该错误
- php二维数组排序方法(array_multisort,usort)
一维数组排序可以使用asort.ksort等一些方法进程排序,相对来说比较简单.二维数组的排序怎么实现呢?使用array_multisort和usort可以实现 例如像下面的数组: $users = ...
- 思考题-关于CSS(转)
dl, dt, dd三个标签浏览器默认margin值多少?是否有标签默认文字粗体? line-height:150%和line-height:1.5的区别是? float为何会让外部容器高度塌陷?这是 ...
- 用bat写的一个小病毒
最近看了一点bat的知识,具体说是看了一个博客:http://blog.csdn.net/qsyzb/article/details/17364581 用了三天才看完=.=,感觉作者整理整理可以把博客 ...
- 【Boost】boost库获取格式化时间
获取时间方式 格式一:YYYYMMDD #include<iostream> #include<string> #include<boost/date_time/greg ...
- ZROI2018提高day3t3
传送门 分析 我们对于每一个可以匹配的字符都将其从栈中弹出,然后他的哈希值就是现在栈中的字符哈希一下.然后我们便可以求出对于哪些位置它们的哈希值是一样的,即它们的状态是一致的.而这些点可以求出它们的贡 ...
- Python程序设计3——字典
1 字典 字典是Python唯一内建的映射类型.字典是键值对的集合. 1.1 字典的使用 某些情况下字典更加好用,比如一个电话列表.注意:电话号码只能用字符串数字表示,否则会出问题.因为电话号码一旦以 ...
- Entity Framework Tutorial Basics(5):Create Entity Data Model
Create Entity Data Model: Here, we are going to create an Entity Data Model (EDM) for SchoolDB datab ...