【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, ...
随机推荐
- Tensorflow mlp二分类
只是简单demo, 可以看出tensorflow非常简洁,适合快速实验 import tensorflow as tf import numpy as np import melt_datas ...
- 【Java EE 学习 48】【Hibernate学习第五天】【抓取策略】【二级缓存】【HQL】
一.抓取策略. 1.hibernate中提供了三种抓取策略. (1)连接抓取(Join Fetch):这种抓取方式是默认的抓取方式.使用这种抓取方式hibernate会在select中内连接的方式获取 ...
- 第六届福建省大学生程序设计竞赛(FZU2213—FZU2221)
from:piaocoder Common Tangents(两圆之间的公公切线) 题目链接: http://acm.fzu.edu.cn/problem.php?pid=2213 解题思路: 告诉你 ...
- 机器学习实战-python相关软件库的安装
1 安装python 2 安装sublime text2 3 安装NumPy.Matplotlib http://book.51cto.com/art/201401/426522.htm Matplo ...
- java中的权限修饰符的理解
首先了解概念: 在java中有四种权限修饰符:范围从大到小分别是:public.protect.default(friendly).private,它们之间的区别是: public: Java语言中访 ...
- Git-将已有的项目提交到Git
准备工作:1. 安装Githttp://git-scm.com/download/2.申请一个GitHub或者coding账号(coding为国产,不需FQ呦.两者方法基本相同,本文以coding为例 ...
- 第01章(认识Java)
/***************** ***认识java第一章 *******知识点: **************1.开发环境搭建 **************2.开发工具使用 ********** ...
- 2012 Multi-University #7
最短路+拆点 A As long as Binbin loves Sangsang 题意:从1走到n,每次都是LOVE,问到n时路径是连续多个"LOVE"的最短距离.秀恩爱不想吐槽. 分析:在普通的最 ...
- js中的call和apply
著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:赵望野链接:https://www.zhihu.com/question/20289071/answer/14745394来源 ...
- [NOIP2011]观光公交 题解
题目大意: 就省了吧 思路: 应该算是贪心. 不难发现,加速只对所有在使用加速器之后连续的一段下车时不用等人的站点下车的人有用.这非常重要. 先算出不加速时的和,并预处理出每个站点最迟到的人的时间.每 ...