【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, ...
随机推荐
- Revit 自定义RibbonPanel到Revit中
项目下找到TheApplication.cs,更改内容: class TheApplication : IExternalApplication { public Result O ...
- C#中对string与string[]的初步操作
开篇之作,简单的对string与string[]进行初步操作,入门篇也,不多说,直接上代码. using System; using System.Collections.Generic; using ...
- windows2008r2的时间同步小结
一.在windows2008r2域控的环境下进行时间同步的配置(当已经拥有可以使用的ntp服务器,并知晓ip,客户端到其网络正常): 客户端的配置过程如下: 1.搜索窗口输入 gpedit.msc 打 ...
- Linux常用命令学习6---(vim的使用)
先说说我,我使用了这么久的vim,但是完全没有将vim的功能完全利用到,无非就是使用了编辑(i).保存(:w).退出(:q).等简单的编辑,命令,以及NerdTree这一个插件,所以在这里需要重新学习 ...
- 使用Lua脚本语言开发出高扩展性的系统,AgileEAS.NET SOA中间件Lua脚本引擎介绍
一.前言 AgileEAS.NET SOA 中间件平台是一款基于基于敏捷并行开发思想和Microsoft .Net构件(组件)开发技术而构建的一个快速开发应用平台.用于帮助中小型软件企业建立一条适合市 ...
- 水晶报表初体验(Visual Studio 2010)
安装水晶报表后如下使用: 配置rpt文件,如图 前台(Asp.net页面): <%@ Register Assembly="CrystalDecisions.Web, Version= ...
- python excel操作
python操作excel表格(xlrd/xlwt)转载:http://www.cnblogs.com/zhoujie/p/python18.html 最近遇到一个情景,就是定期生成并发送服务器使 ...
- inner join on 和 where = 的区别!
请看下面两条语句:select * from table1 inner join table2 on table1.id = table2.idselect * from table1,table2 ...
- 2016中国大学生程序设计竞赛 网络选拔赛 I This world need more Zhu
This world need more Zhu Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
- 2016-2017 ACM-ICPC, NEERC, Moscow Subregional Contest
A. Altitude 从小到大加入每个数,用set查找前驱和后继即可. 时间复杂度$O(n\log n)$. #include <bits/stdc++.h> using namespa ...