乘风破浪:LeetCode真题_003_Longest Substring Without Repeating Characters

一、前言

在算法之中出现最多的就是字符串方面的问题了,关于字符串的模式匹配,回文字符串,等等问题都是非常经典的,同样的在字符串之中寻找最长的不重复的字符串的长度也是比较有意义的。

二、LeetCode真题_003_Longest Substring Without Repeating Characters

2.1 问题

2.2 分析与解决

问题是非常清晰的,我们最先想到的就是使用暴力算法,通过读取当前的字符和已经读取过的字符进行比较,如果冲突则调整开始的位置,并记录当前的最大长度,直至遍历结束,这样的时间复杂度就要到达O(n~3)了。那么有没有比较好的解决方案的,我们又想到了hash算法,这样就省去了比较需要花费的遍历时间,增加了程序需要占用的空间。

    首先看看官方的作答:

    暴力算法:

public class Solution {
public int lengthOfLongestSubstring(String s) {
int n = s.length();
int ans = 0;
for (int i = 0; i < n; i++)
for (int j = i + 1; j <= n; j++)
if (allUnique(s, i, j)) ans = Math.max(ans, j - i);
return ans;
} public boolean allUnique(String s, int start, int end) {
Set<Character> set = new HashSet<>();
for (int i = start; i < end; i++) {
Character ch = s.charAt(i);
if (set.contains(ch)) return false;
set.add(ch);
}
return true;
}
}

   使用HashSet:

public class Solution {
public int lengthOfLongestSubstring(String s) {
int n = s.length();
Set<Character> set = new HashSet<>();
int ans = 0, i = 0, j = 0;
while (i < n && j < n) {
// try to extend the range [i, j]
if (!set.contains(s.charAt(j))){
set.add(s.charAt(j++));
ans = Math.max(ans, j - i);
}
else {
set.remove(s.charAt(i++));
}
}
return ans;
}
}

   使用HashMap:

public class Solution {
public int lengthOfLongestSubstring(String s) {
int n = s.length(), ans = 0;
Map<Character, Integer> map = new HashMap<>(); // current index of character
// try to extend the range [i, j]
for (int j = 0, i = 0; j < n; j++) {
if (map.containsKey(s.charAt(j))) {
i = Math.max(map.get(s.charAt(j)), i);
}
ans = Math.max(ans, j - i + 1);
map.put(s.charAt(j), j + 1);
}
return ans;
}
}

   假设只有ASCII的字符出现,没有中文:

public class Solution {
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;
}
}

   我们的算法:

 import java.util.Arrays;
import java.util.HashMap;
import java.util.Map; public class Solution { // 可以处理所有的UTF-8字符
public int lengthOfLongestSubstring(String s) {
// 字符串输入不合法
if (s == null) {
return 0;
} // 当前处理的开始位置
int start = 0;
// 保存结果
int result = 0;
// 访问标记,记录最新一次访问的字符和位置
Map<Character, Integer> map = new HashMap<>(s.length()); for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
// 如果字符已经出现过(在标记开位置算起),就重新标记start
if (map.containsKey(ch) && map.get(ch) >= start) {
start = map.get(ch) + 1;
}
// 如果没有出现过就求最大的非重复子串的长度
else {
result = Math.max(result, i - start + 1);
} // 更新访问记录
map.put(ch, i);
}
return result;
} // 只考虑ASCII字符
public int lengthOfLongestSubstring2(String s) {
// 字符串输入不合法
if (s == null) {
return 0;
} // 标记字符是否出现过,并且记录是的最新一次访问的元素的位置
int[] appear = new int[256];
// 初始化为-1
Arrays.fill(appear, -1);
// 当前处理的开始位置
int start = 0;
// 保存结果
int result = 0; for (int i = 0; i < s.length(); i++) {
// 如果字符已经出现过(在标记开位置),就重新标记start
if (appear[s.charAt(i)] >= start) {
start = i + 1;
}
// 如果没有出现过就求最大的非重复子串的长度
else {
result = Math.max(result, i - start + 1);
}
// 标记第i个字符已经被访问过(最新是第i个位置)
appear[s.charAt(i)] = i;
} return result;
}
}

三、总结

字符串方面的问题我们要非常熟悉String的一些工具函数,以及考虑到hash算法的数据结构来优化和解决问题。

乘风破浪:LeetCode真题_003_Longest Substring Without Repeating Characters的更多相关文章

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

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

  2. leetcode第三题--Longest Substring Without Repeating Characters

    Problem:Given a string, find the length of the longest substring without repeating characters. For e ...

  3. leetcode第三题Longest Substring Without Repeating Characters java

    Longest Substring Without Repeating Characters Given a string, find the length of the longest substr ...

  4. LeetCode第三题—— Longest Substring Without Repeating Characters(最长无重复子字符串)

    题目描述 Given a string, find the length of the longest substring without repeating characters. Example ...

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

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

  6. 【LeetCode OJ】Longest Substring Without Repeating Characters

    题目链接:https://leetcode.com/problems/longest-substring-without-repeating-characters/ 题目:Given a string ...

  7. 【LeetCode】003. Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

  8. 【LeetCode】3. Longest Substring Without Repeating Characters 无重复字符的最长子串

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:无重复字符,最长子串,题解,leetcode, 力扣,py ...

  9. 【LeetCode】3.Longest Substring Without Repeating Characters 最长无重复子串

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

随机推荐

  1. Jquery 在多个相同标签click的问题

    最近在做文章的删除动作,用Jquery来执行操作.但是实现时一开始总是只能对第一个起作用,其他的点击删除后没反应. 一开始的jquery代码是这样的, $('#articledelete').on(' ...

  2. golang-开发配置

    环境变量配置 Windows GOROOT GOROOT = D:\ProgramDev\Go GOROOT GOROOT = D:\WorkSpace\goProjects GOBIN GOBIN ...

  3. Angularjs跨域

    一.首先我们要明白跨域的字面概念,读过留过印象之后,下面将会有例子进一步解释 有一篇文章<跨域的理解与实现>描述得很清楚,在这里摘录如下: 域(Domain)是Windows网络中独立运行 ...

  4. C/C++编码规范

    C/C++编码规范 今天人们越来越明白软件设计更多地是一种工程,而不是一种个人艺术.由于大型产品的开发通常由很多的人协同作战,如果不统一编程规范,最终合到一起的程序,其可读性将较差,这不仅给代码的理解 ...

  5. iOS开源项目周报0302

    由OpenDigg 出品的iOS开源项目周报第十期来啦.我们的iOS开源周报集合了OpenDigg一周来新收录的优质的iOS开源项目,方便iOS开发人员便捷的找到自己需要的项目工具等.TodayMin ...

  6. [转]a-mongodb-tutorial-using-c-and-asp-net-mvc

    本文转自:http://www.joe-stevens.com/2011/10/02/a-mongodb-tutorial-using-c-and-asp-net-mvc/ In this post ...

  7. SmartGit破解使用的个人方法

    转自:https://www.cnblogs.com/nn839155963/p/5912788.html SmartGit是收费的,可以30天的试用期,30天试用期过后,smartgit 需要输入序 ...

  8. 标准Trie字典树学习一:原理解析

    特别声明: 博文主要是学习过程中的知识整理,以便之后的查阅回顾.部分内容来源于网络(如有摘录未标注请指出).内容如有差错,也欢迎指正! 系列文章: 1. 字典树Trie学习一:原理解析 2.字典树Tr ...

  9. django中的验证码

    from django.shortcuts import renderfrom PIL import Imagefrom PIL import ImageDrawfrom PIL import Ima ...

  10. hibernate的inverse用法

    Inverse和cascade是Hibernate映射中最难掌握的两个属性.两者都在对象的关联操作中发挥作用. 1.明确inverse和cascade的作用 inverse 决定是否把对对象中集合的改 ...