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的更多相关文章

  1. 【LeetCode】Longest Substring Without Repeating Characters 解题报告

    [题意] Given a string, find the length of the longest substring without repeating characters. For exam ...

  2. 【leetcode】Longest Substring Without Repeating Characters

    题目描述: Given a string, find the length of the longest substring without repeating characters. For exa ...

  3. 【leetcode】Longest Substring Without Repeating Characters (middle)

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  4. 【Leetcode】【Medium】Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  5. 【LeetCode3】Longest Substring Without Repeating Characters★★

    题目描述: 解题思路: 借用网上大神的思想:the basic idea is, keep a hashmap which stores the characters in string as key ...

  6. 【LeetCode】Longest Substring Without Repeating Characters(无重复字符的最长子串)

    这道题是LeetCode里的第3道题. 题目描述: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: ...

  7. 【LeetCode刷题系列 - 003题】Longest Substring Without Repeating Characters

    题目: Given a string, find the length of the longest substring without repeating characters. Example 1 ...

  8. 【LeetCode每天一题】Longest Substring Without Repeating Characters(最长无重复的字串)

    Given a string, find the length of the longest substring without repeating characters. Example 1:    ...

  9. 【LeetCode OJ】Longest Substring Without Repeating Characters

    题目链接:https://leetcode.com/problems/longest-substring-without-repeating-characters/ 题目:Given a string ...

  10. 【leetcode刷题笔记】Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. For example, ...

随机推荐

  1. struts2+jsp+hiberbate 双重遍历

    今天弄了个双重遍历,由于自己水平有限,做了好久才搞定. 例子如下:也不算是例子,简要代码吧 action中有属性: private List<Contents> content; 其中co ...

  2. VR技术的探索阶段

    转载请声明转载地址:http://www.cnblogs.com/Rodolfo/,违者必究. 早在1929年,在长期使用教练机训练器(机翼变短,不能产生离开地面所需的足够提升力)进行飞行训练之后,E ...

  3. Python爬虫Scrapy框架入门(1)

    也许是很少接触python的原因,我觉得是Scrapy框架和以往Java框架很不一样:它真的是个框架. 从表层来看,与Java框架引入jar包.配置xml或.property文件不同,Scrapy的模 ...

  4. 微信开发笔记:公众号获取access_token

    微信开发中,access_token的获取是一种非常常见的功能,通过公众号的appid和appsecret来向微信公众平台请求一个临时通行凭证:access_token.公众平台上的绝大部分操作都会需 ...

  5. poj 3253 Fence Repair

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 42979   Accepted: 13999 De ...

  6. js添加var和不加var区别

    var 声明的变量,作用域是当前 function 没有声明的变量,直接赋值的话, 会自动创建变量 但作用域是全局的. //----------------- function doSth() { a ...

  7. 2016 Multi-University Training Contest 4

    6/12 2016 Multi-University Training Contest 4官方题解 KMP+DP A Another Meaning(CYD) 题意: 给一段字符,同时给定你一个单词, ...

  8. BZOJ2471 : Count

    考虑KMP,设$f[i][j][S]$表示还剩最低$i$位没有确定,目前KMP匹配到了$j$这个位置,前缀匹配情况是$S$,最终会匹配到哪里,中途匹配成功几次. 其中$S[i]$是一个pair< ...

  9. MSSQL跨服务器插入

    跨服器插入数据  其中服务器的IP为源数据地址 还要注意的是目标库中不能存在对应表. select * INTO compMaintype from openrowset('SQLOLEDB' , ' ...

  10. websocket 实现聊天功能

    <html> <head> <base href="<%=basePath%>"> <title>webscoket t ...