原题链接: http://oj.leetcode.com/problems/longest-substring-without-repeating-characters/ 
这道题用的方法是在LeetCode中很常用的方法,对于字符串的题目非常有用。 首先brute force的时间复杂度是O(n^3), 对每个substring都看看是不是有重复的字符,找出其中最长的,复杂度非常高。优化一些的思路是稍微动态规划一下,每次定一个起点,然后从起点走到有重复字符位置,过程用一个HashSet维护当前字符集,认为是constant操作,这样算法要进行两层循环,复杂度是O(n^2)。

最后,我们介绍一种线性的算法,也是这类题目最常见的方法。基本思路是维护一个窗口,每次关注窗口中的字符串,在每次判断中,左窗口和右窗口选择其一向前移动。同样是维护一个HashSet, 正常情况下移动右窗口,如果没有出现重复则继续移动右窗口,如果发现重复字符,则说明当前窗口中的串已经不满足要求,继续移动有窗口不可能得到更好的结果,此时移动左窗口,直到不再有重复字符为止,中间跳过的这些串中不会有更好的结果,因为他们不是重复就是更短。因为左窗口和右窗口都只向前,所以两个窗口都对每个元素访问不超过一遍,因此时间复杂度为O(2*n)=O(n),是线性算法。空间复杂度为HashSet的size,也是O(n). 代码如下:

public int lengthOfLongestSubstring(String s) {
if(s==null || s.length()==)
return ;
HashSet<Character> set = new HashSet<Character>();
int max = ;
int walker = ;
int runner = ;
while(runner<s.length())
{
if(set.contains(s.charAt(runner)))
{
if(max<runner-walker)
{
max = runner-walker;
}
while(s.charAt(walker)!=s.charAt(runner))
{
set.remove(s.charAt(walker));
walker++;
}
walker++;
}
else
{
set.add(s.charAt(runner));
}
runner++;
}
max = Math.max(max,runner-walker);
return max;
}

这道题思想在字符串处理的题中还是比较重要的,实现上主要是HashSet和数组index的操作。扩展的题目有Substring with Concatenation of All WordsMinimum Window Substring,思路是非常接近的,只是操作上会更加繁琐一些。

Longest Substring Without Repeating Characters -- LeetCode的更多相关文章

  1. Longest Substring Without Repeating Characters leetcode java

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

  2. Longest Substring Without Repeating Characters ---- LeetCode 003

    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. For example, ...

  4. leetcode: longest substring without repeating characters

    July 16, 2015 Problem statement: Longest Substring Without Repeating Characters Read the blog: http: ...

  5. LeetCode 3 Longest Substring Without Repeating Characters(最长不重复子序列)

    题目来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, f ...

  6. LeetCode 3 Longest Substring Without Repeating Characters 解题报告

    LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...

  7. LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters

    LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...

  8. [LeetCode][Python]Longest Substring Without Repeating Characters

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...

  9. 【一天一道LeetCode】 #3 Longest Substring Without Repeating Characters

    一天一道LeetCode (一)题目 Given a string, find the length of the longest substring without repeating charac ...

随机推荐

  1. 错误信息:System.Resources.MissingManifestResourceException: 未能找到任何适合于指定的区域或非特定区域性的资源。请确保在编译时已将“****.****.Resource.resources”正确嵌入或链接到程序集"****",或者确保所有需要的附属程序集都可加载并已进行了完全签名

    在网上搜索了N久都没看到几篇解决的文章,最后在不懈的努力下终于解决了,所以决定写下解决方法方便以后遇到同样问题的朋友: 其实这个错误的主要问题就是没有找到需要的资源文件(该文件为Resources.r ...

  2. 32个触发事件XSS语句的总结

    1.onmouseenter:当鼠标进入选区执行代码 <div style="background-color:red" onmouseenter="alert(b ...

  3. Mutual Training for Wannafly Union #1解题报告

    ---恢复内容开始--- q神等人组织的vjudge上的多校训练,题目基本上都来自于CF,#1是上周进行的,参加后感觉收获很多,因为上周准备期中比较忙,解题报告现在补上. 比赛地址(兼题目地址) A题 ...

  4. SqlServer调用CLR服务

    作用 从数据库发起对外部服务的请求. 应用场景 心跳监测 定时启动站点 服务实现 代码部分(C#) using System; using System.Net; publicpartialclass ...

  5. linux命令每日一练习-mkdir,rm

    mkdir 创建一个文件夹 mkdir -p tian/hong 递归创建文件目录 mkdir tian hong 创建多个文件 mkdir -v tian 创建文件的时候显示信息 mkdir -m ...

  6. 保留json字符串中文的函数,代替json_encode

    // 格式化json中的汉字函数    protected function encode_json($str) {        $strs = urldecode(json_encode($thi ...

  7. mysql主从复制配置(精简版)

    一.首先准备两台服务器,虚拟机即可,以笔者为例:master:192.168.1.105 slave:192.168.1.106 二.保证两台虚拟机能相互ping通,先把防火墙关闭:service i ...

  8. SPOJ BALNUM

    一开始题看错了...dp[pos][sets][viss],其中sets表示出现次数,viss表示出现没有. #include<iostream> #include<cstdio&g ...

  9. HDU5128 细心、细心、细心

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5128 题意:给你n(n < 30)个点的坐标,然后让你求出这n个点能构成的两个最大矩形的面积,有 ...

  10. ubuntu14.04 键盘错位小问题

    分享一个ubuntu14.04里的小问题,不知道有没有人跟我一样悲催 问题: 键盘错位了,@和"两个符合正好颠倒,~#这几个也错乱了,~符号跑到了|上,#跑到了shift+|上,这个问题在敲 ...