【leedcode】longest-substring-without-repeating-characters
Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which the length is . Given "bbbbb", the answer is "b", with the length of . Given "pwwkew", the answer is "wke", with the length of . Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
寻找最长的不重复串,略像滑动窗口。
char[] buff;
//used
int used ;
public int lastIndexOf(char ch, int endIndex) {
int i = used;
for (; i >= endIndex; i--) {
if (buff[i] == ch) {
return i;
}
}
return -1;
}
public int lastIndexOf(char ch) {
return lastIndexOf(ch, 0);
} public int lengthOfLongestSubstring(String s) {
if (s == null || s.isEmpty()) {
return 0;
}
int len = s.length();
buff = new char[len];
buff[0] = s.charAt(0);
used = 1;
char t;
int idx = -1;
int top = 0;
int first = 0;
while(used<len){
t = s.charAt(used);
idx = lastIndexOf(t, first);
// System.err.println("["+top+"]s=" + new String(buff) + "," + (idx>-1 ? ( "pos=" + idx + " find " + t ) : "" ) + ",idx=" + used + ",first=" + first); if (idx > -1) {
top = Math.max(used - first, top);
first = idx+1;
} buff[used] = t;
used++;
} // System.err.println("["+top+"]s=" + new String(buff) + "," + (idx>-1 ? ( "pos=" + idx + " find " + t ) : "" ) + ",idx=" + used + ",first=" + first);
return Math.max(len - first, top);
}
上面这个粗鲁的代码,马马虎虎毕竟打败51 ~61%的code,lastIndexOf有优化的空间。
不过答案真是美妙的活动窗口实现,赞!使用了字符作为坐标,索引作为值。
public int lengthOfLongestSubstring(String s) {
int n = s.length(), ans = 0;
int[] index = new int[128]; // 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;
}
【leedcode】longest-substring-without-repeating-characters的更多相关文章
- 【LeetCode】Longest Substring Without Repeating Characters 解题报告
[题意] Given a string, find the length of the longest substring without repeating characters. For exam ...
- 【leetcode】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 (middle)
Given a string, find the length of the longest substring without repeating characters. For example, ...
- 【Leetcode】【Medium】Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
- 【LeetCode3】Longest Substring Without Repeating Characters★★
题目描述: 解题思路: 借用网上大神的思想:the basic idea is, keep a hashmap which stores the characters in string as key ...
- 【LeetCode】Longest Substring Without Repeating Characters(无重复字符的最长子串)
这道题是LeetCode里的第3道题. 题目描述: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: ...
- 【LeetCode刷题系列 - 003题】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. Example 1: ...
- 【LeetCode OJ】Longest Substring Without Repeating Characters
题目链接:https://leetcode.com/problems/longest-substring-without-repeating-characters/ 题目:Given a string ...
- 【leetcode刷题笔记】Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
随机推荐
- HTML5本地存储——Web SQL Database
在HTML5 WebStorage介绍了html5本地存储的Local Storage和Session Storage,这两个是以键值对存储的解决方案,存储少量数据结构很有用,但是对于大量结构化数据就 ...
- [转]js 将图片连接转换称base64格式
参考:http://blog.csdn.net/wyyfwm/article/details/45917255 我们把图像文件的内容直接写在了HTML 文件中,这样做的好处是,节省了一个HTTP 请求 ...
- 宿主机ping不通虚拟机cenos7
参考网址1:http://zhidao.baidu.com/link?url=2v3NXGyzPT-XTYwon8PesZLnMg02Ako6nDub3vJiJt4miSmkOA-04xLUqfu9s ...
- Apache, Tomcat, JK Configuration Example
Example of worker.properties: worker.list=myWorker,yourWorker worker.myWorker.port=7505 worker.myWor ...
- Windows Server 2008 R2 DNS 服务器迁移
- 2014亚马逊在线笔试题目及解决方案(MMChess问题)
整体思路:关键是需要知道当前Steps数组中的全排列即可,而且需要不重复的全排列.即关键在于非递归的全排列实现即可~ 其实直接利用STL中的next_permutation算法的,这里我又自己实现了一 ...
- 最小生成树のprim算法
Problem A Time Limit : 1000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Sub ...
- Odoo Graph 指定默认 类型
<graph string='Sale Paid Grapg' type="pivot"> <field name='section_id' type=" ...
- c++字符串
之所以抛弃char*的字符串而选用C++标准程序库中的string类,是因为他和前者比较起来,不必 担心内存是否足够.字符串长度等等,而且作为一个类出现,他集成的操作函数足以完成我们大多数情况下(甚至 ...
- 初用idea建立javaweb遇到的问题与心得
1.直接用idea建立的web项目,其自动生成的web.xml里version=3.1,这样的话建立servlet-name等标签会报错(因为3.1不支持这种做法,更提倡用注解的办法),解决办法是将w ...