题目大意是传入一条字符串,计算出这样的这样一条子字符串,要求子字符串是原字符串的连续的某一段,且子字符串内不包含两个或两个以上的重复字符。求符合上面条件的字符串中最长的那一条的长度。


首先注意到任意一条无重复子字符串的任意子字符串应该也满足无重复这一特性。因此这个问题可以用动态规划解决。

需要先计算出字符串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];}
}

转载于:https://www.cnblogs.com/dalt/p/6940808.html

Leetcode: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 example, ...

  3. LeetCode——Longest Substring Without Repeating Characters

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

  4. LeetCode Longest Substring Without Repeating Characters

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

  5. [LeetCode]3.Longest Substring Without Repeating Characters

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

  6. LeetCode.3-最长无重复字符子串(Longest Substring Without Repeating Characters)

    这是悦乐书的第341次更新,第365篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Medium级别的第2题Longest Substring Without Repeating Cha ...

  7. LeetCode:3. Longest Substring Without Repeating Characters

    https://leetcode.com/problems/longest-substring-without-repeating-characters/description/ 内容描述: Give ...

  8. LeetCode 第 3 题(Longest Substring Without Repeating Characters)

    LeetCode 第 3 题(Longest Substring Without Repeating Characters) Given a string, find the length of th ...

  9. leetcode(三)—— Longest Substring Without Repeating Characters(最长不重复子串 Python/C++)

    Longest Substring Without Repeating Characters | LeetCode OJ 使用 hash 判重问题首先想到的就是 hash(或者使用 map): 思路: ...

  10. 【贪心】LeetCode 3. Longest Substring Without Repeating Characters

    LeetCode 3. Longest Substring Without Repeating Characters Solution1:我的答案 该方法中哈希表记录的是字符出现的次数.标准的贪心算法 ...

最新文章

  1. ajax对服务器路径请求
  2. c++ fstream用得多不?_护肤品在精不在多,简单3步:用对产品皮肤好,真正会护肤的都懂...
  3. 使用JavaFX AnimationTimer
  4. java dubbo 方案,Missing artifact com.alibaba:dubbo:jar:2.8.4 dubbo解决方案
  5. ubuntu 14 java web服务器搭建
  6. python fun函数、求4x4整型数组的主对角线元素的和_python中多维数组中列major的numpy整形...
  7. 利用R和Octave绘制函数图像和求解方程
  8. Spring : spring的aware
  9. 数据库原理--事务(一)
  10. vue项目中使用lib-flexible解决移动端适配
  11. AcWing 894. 拆分-Nim游戏
  12. 搞懂 Vision Transformer 原理和代码,看这篇技术综述就够了(五)
  13. 湖南师范大学计算机网络基础教学平台,基于网络的师徒式教学平台的设计与实现...
  14. day04-商城后台搭建
  15. Windows聚焦壁纸保存方法
  16. 支付宝android 10.0.8,分享10.0.8版支付宝开启和关闭小额免密支付功能方法
  17. 类似win7系统泡泡屏保
  18. 【精读笔记】JavaScript高级程序设计 第3章 语言基础
  19. Android学习之登陆界面设计(二)基本界面设计
  20. 《经济学是什么》精髓:如何用经济学家的眼光理解个人选择和市场经济?

热门文章

  1. 【今日所得】1.29。。。
  2. Windows Phone 7 检查手机网络
  3. [转载]在vim中针对c++自动补全
  4. 关于c# 静态构造函数的说明
  5. 历史文章之机器学习和深度学习
  6. seaborn—seaborn.distplot绘制直方图和连续密度统计
  7. LeetCode刷题——64. 最小路径和
  8. 直播实录丨十年主导15个产品从0到1,她的经验与思考现场拆解
  9. 每周荐书:京东架构、Linux内核、Python全栈
  10. 反思设计——从大师身上反思