乘风破浪:LeetCode真题_003_Longest Substring Without Repeating Characters
乘风破浪: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的更多相关文章
- LeetCode 第 3 题(Longest Substring Without Repeating Characters)
LeetCode 第 3 题(Longest Substring Without Repeating Characters) Given a string, find the length of th ...
- leetcode第三题--Longest Substring Without Repeating Characters
Problem:Given a string, find the length of the longest substring without repeating characters. For e ...
- leetcode第三题Longest Substring Without Repeating Characters java
Longest Substring Without Repeating Characters Given a string, find the length of the longest substr ...
- LeetCode第三题—— Longest Substring Without Repeating Characters(最长无重复子字符串)
题目描述 Given a string, find the length of the longest substring without repeating characters. Example ...
- 【一天一道LeetCode】 #3 Longest Substring Without Repeating Characters
一天一道LeetCode (一)题目 Given a string, find the length of the longest substring without repeating charac ...
- 【LeetCode OJ】Longest Substring Without Repeating Characters
题目链接:https://leetcode.com/problems/longest-substring-without-repeating-characters/ 题目:Given a string ...
- 【LeetCode】003. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- 【LeetCode】3. Longest Substring Without Repeating Characters 无重复字符的最长子串
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:无重复字符,最长子串,题解,leetcode, 力扣,py ...
- 【LeetCode】3.Longest Substring Without Repeating Characters 最长无重复子串
题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...
随机推荐
- C#控制台程序,运行完窗口不退出的方法
.... static void Main(string[] args){ Console.WriteLine("运行完后不退出窗口"); Console.ReadKey();// ...
- [转载]二叉树(BST,AVT,RBT)
二叉查找树(Binary Search Tree)是满足如下性质的二叉树:①若它的左子树非空,则左子树上所有结点的值均小于根结点的值:②若它的右子树非空,则右子树上所有结点的值均大于根结点的值:③左. ...
- Linux原始套接字实现分析---转
http://blog.chinaunix.net/uid-27074062-id-3388166.html 本文从IPV4协议栈原始套接字的分类入手,详细介绍了链路层和网络层原始套接字的特点及其内核 ...
- 关于Hall定理的学习
基本定义 \(Hall\) 定理是二分图匹配的相关定理 用于判断二分图是否存在完美匹配 存在完美匹配的二分图即满足最大匹配数为 \(min(|X|,|Y|)\) 的二分图,也就是至少有一边的点全部被匹 ...
- 最新的 Vue 相关开源项目库汇总
优秀的vue 开源后台管理开源系统框架 https://panjiachen.github.io/vue-element-admin-site/#/zh-cn/README UI组件 开发框架 实用库 ...
- 如何在service实现弹出对话框
因为一些需求,我想在service处理后台运行时候,会弹出对话框,但是对话框的建立需要传入Context的值,我试过传入this,也就是service自己的context,还有 传入ge ...
- 阿里云服务器windows server流量不大的情况下,tomcat经常出现访问阻塞,手动ctrl+c或者点击右键又访问正常
我被这个问题折磨了好几天,因为这两天要帮别人做推广,不能再出现这样的情况了,不然广告费就白烧了,所以特意查了一下资料,结果解决方案被我找出来了. 问题发生原因是因为打开编辑选项后,一不小心点到dos窗 ...
- org.apache.commons.lang.StringUtils
org.apache.commons.lang.StringUtils 作为jdk中lang包的补充 检查CharSequence是否为空,null或者空格 CharSequence (CharBuf ...
- 【SSH网上商城项目实战22】获取银行图标以及支付页面的显示
转自: https://blog.csdn.net/eson_15/article/details/51452243 从上一节的小demo中我们搞清楚了如何跟易宝对接以及易宝的支付流程.这一节 ...
- js类的笔记
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...