Leetcode:Longest Substring Without Repeating Characters分析和实现
题目大意是传入一条字符串,计算出这样的这样一条子字符串,要求子字符串是原字符串的连续的某一段,且子字符串内不包含两个或两个以上的重复字符。求符合上面条件的字符串中最长的那一条的长度。
首先注意到任意一条无重复子字符串的任意子字符串应该也满足无重复这一特性。因此这个问题可以用动态规划解决。
需要先计算出字符串s的每个元素对应的首个重复字符的下标,字符串中下标为i的元素的首个重复字符的下标j应该满足j > i && s[i] == s[j],且是满足这些条件中的最小值。比如asaa中下标为0的元素对应的首个重复字符的下标为2,尽管3也满足条件,但不是最小值。
计算的方式如下:
registries = int[256]
nextOccurance = int[s.length]
initialize elements in registries with -1 //将所有registries 中的元素都初始化为-1
initialize elements in nextOccurance with s.length //将所有registries 中的元素都初始化为s.length
for(i = 0; i < s.length; i++)
c = s[i]
if registries[c] != -1 then
nextOccurance [registries[c]] = i
registries[c] = i
这里用注册表registries的注册项registries[c]记录当前对字符c的查找请求,而nextOccurance的元素nextOccurance[i]表示s[i]的首个重复字符子s中的下标。每次循环时,都会检测注册表项registries[c]是否以及被注册,如果注册则为nextOccurance [registries[c]]记录当前下标i。最后则将之前的注册项清除,而用当前下标作为新的请求项。
之后使用动态规划解决问题。利用数组longestLengthes,longestLengthes[i]表示所有以下标i作为起始的无重复子字符串的最长长度。显然longestLengthes[s.length - 1]应该为1。而对于任意i < s.length - 1,应该有longestLengthes[i] = min(longestLengthes[i + 1] + 1, nextOccurance [i] - i)。其中longestLengthes[i + 1] + 1表示理想状态下(不考虑出现多个longestLengthes[i]对应的字符)的最优长度,而nextOccurance [i] - i表示可能的以下标i作为起始的无重复子字符串的最长长度,因为s[i] == s[nextOccurance [i]],这里已经发生了重复。之所以敢保证longestLengthes[i] <= longestLengthes[i + 1] + 1是因为假如longestLengthes[i]>longestLengthes[i + 1] + 1,那么显然longestLengthes[i + 1] >= longestLengthes[i] - 1 >longestLengthes[i + 1](整体不重复自然局部不会重复),这是不可能发生的。(如果对这部分不理解,可以枚举可能的情况,总共只有三种)转换成代码:
longestLengthes = int[s.length]
longestLengthes[s.length - 1] = 1
for(i = s.length - 2; i >= 0; i--)
longestLengthes[i] = min(longestLengthes[i + 1] + 1, nextOccurance [i] - i)
之后遍历整个longestLengthes数组就可以找到最长的无重复子字符串的长度。
把上面两部分的代码整合,可以轻易得出整体的复杂度为O(n),其中n为传入字符串的长度。
最后给出整个实现代码,给有需要的人:
package cn.dalt.leetcode;
/**
* Created by Administrator on 2017/6/4.
*/
public class LongestSubstringWithoutRepeatingCharacters {
public static void main(String[] args) {
String s = "yiwgczzovxdrrgeebkqliobitcjgqxeqhbxkcyaxvdqplxtmhmarcbzwekewkknrnmdpmfohlfyweujlgjf";
System.out.println(new LongestSubstringWithoutRepeatingCharacters().lengthOfLongestSubstring(s));
for(char c = 0; c < 256; c++)
{
System.out.println((int)c + ":" + c);
}
}
int[] nextOccurIndexes = null;
int[] registries = new int[1 << 8];
public int lengthOfLongestSubstring(String s) {
//Calculate all next occur indexes
//nextOccurIndexes[i] is the minimun index of s which has properties that index > i && s[i] = s[index]
int slength = s.length();
if (slength == 0) {
return 0;
}
nextOccurIndexes = new int[s.length()];
for (int i = 0, bound = registries.length; i < bound; i++) {
registries[i] = -1;
}
for (int i = 0, bound = s.length(); i < bound; i++) {
int c = s.charAt(i);
int registry = registries[c];
if (registry != -1) {
nextOccurIndexes[registry] = i;
}
registries[c] = i;
}
for (int registry : registries) {
if (registry != -1) {
nextOccurIndexes[registry] = slength;
}
}
int[] longestNoneRepetitionSubstringLengthes = new int[s.length()];
longestNoneRepetitionSubstringLengthes[s.length() - 1] = 1;
int longestNoneRepetitionSubstringIndex = s.length() - 1;
for (int i = s.length() - 2; i >= 0; i--) {
int probablyMaxLength1 = longestNoneRepetitionSubstringLengthes[i + 1] + 1;
int probablyMaxLength2 = nextOccurIndexes[i] - i;
longestNoneRepetitionSubstringLengthes[i] = Math.min(probablyMaxLength1, probablyMaxLength2);
longestNoneRepetitionSubstringIndex =
longestNoneRepetitionSubstringLengthes[i] > longestNoneRepetitionSubstringLengthes[longestNoneRepetitionSubstringIndex] ?
i : longestNoneRepetitionSubstringIndex;
}
return longestNoneRepetitionSubstringLengthes[longestNoneRepetitionSubstringIndex];
}
}
Leetcode: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 example, ...
- leetcode: longest substring without repeating characters
July 16, 2015 Problem statement: Longest Substring Without Repeating Characters Read the blog: http: ...
- 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. Example 1: In ...
- C++ leetcode Longest Substring Without Repeating Characters
要开学了,不开森.键盘声音有点大,担心会吵到舍友.今年要当个可爱的技术宅呀~ 题目:Given a string, find the length of the longest substring w ...
- [LeetCode]Longest Substring Without Repeating Characters题解
Longest Substring Without Repeating Characters: Given a string, find the length of the longest subst ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串 C++实现java实现
最长无重复字符的子串 Given a string, find the length of the longest substring without repeating characters. Ex ...
- LeetCode——Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
- [Leetcode] Longest Substring Without Repeating Characters (C++)
题目: Given a string, find the length of the longest substring without repeating characters. For examp ...
随机推荐
- Android快速开发-选项卡
介绍 几行代码实现Android选项卡界面,支持标准底部Tab自定义视图选项卡,头部文字选项卡. 底部自定义视图选项卡 先来看看实现下图中的效果我们代码应该怎么写? 实现上图效果只需以下代码: pub ...
- findContours()的使用崩溃问题
之前用的好好的,不知咱弄得就突然崩溃.然后网上搜了半天. 各种试,不行. 有一种改变代码方式的做法,可行,但是心里用着很是不爽.vector<vector<Point>>con ...
- AS3舞台的大小,可视区域大小及SWF文件的原始尺寸大小
AS3舞台的大小,可视区域大小及SWF文件的原始尺寸大小三者之间没有什么关系. 当前可视区域:stage.stageWidth,stage.stageHeight.SWF文件编译后的原始尺寸大小:lo ...
- ORM 关键
1. 老师的增删改查 1. teacher_obj.cid.add(*[1, 2, 3]) 添加(必须打散) 2. teacher_obj.cid.set([1, 2, 3]) 设置(不用打散) 2. ...
- [转载] FFMPEG结构体分析:AVFrame
注:写了一系列的结构体的分析的文章,在这里列一个列表: FFMPEG结构体分析:AVFrameFFMPEG结构体分析:AVFormatContextFFMPEG结构体分析:AVCodecContext ...
- Visual Studio 2013 帮助文档 安装以及如何直接打开
1.在线安装VS2013的MSDN帮助文档 在利用VS2013集成开发环境(IDE)开发程序代码时会经常用到帮助文档,但默认情况下在帮助文档是在线以网页的形式呈现的,当我们不方便上网时就不能够查看帮助 ...
- LOJ#2351. 「JOI 2018 Final」毒蛇越狱
LOJ#2351. 「JOI 2018 Final」毒蛇越狱 https://loj.ac/problem/2351 分析: 首先有\(2^{|?|}\)的暴力非常好做. 观察到\(min(|1|,| ...
- verilog中task的用法
任务就是一段封装在“task-endtask”之间的程序.任务是通过调用来执行的,而且只有在调用时才执行,如果定义了任务,但是在整个过程中都没有调用它,那么这个任务是不会执行的.调用某个任务时可能需要 ...
- Jquery3.x高版本支持IE8
最近在做项目的时候,遇到一个安全漏洞的问题 检测到目标站点存在javascript框架库漏洞 解决办法是 将受影响的javascript框架库升级到最新版本. 好吧,就给你升吧,升完之后,我的天啊,尽 ...
- 在TreeView 控件上,如果双击任何一个节点的checkbox 只会收到一次After_Check事件 但是check属性变化两次(从false到true 再从true到false),请问该如何解决,谢谢!
在TreeView 控件上,如果双击任何一个节点的checkbox 只会收到一次After_Check事件 但是check属性变化两次(从false到true 再从true到false),请问该如何解 ...