题目:

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

示例:

Given "abcabcbb", the answer is "abc", which the length is 3.

Given "bbbbb", the answer is "b", with the length of 1.

Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.

问题的难点在于光标移动过程中,一旦遇到重复需要重新开始计算长度

第一次提交的代码如下:

class Solution {
public int lengthOfLongestSubstring(String s) {
int longestLength = 0;
int currentLength = 0;
String longestStr = "";
char tmpChar;
int repeatIndex = 0; for(int i = 0; i < s.length(); i++){
tmpChar = s.charAt(i);
repeatIndex = longestStr.indexOf(tmpChar, 0);
// 遇到重复,从重复的下一位开始
if(repeatIndex >= 0) {
if(longestLength < currentLength){
longestLength = currentLength;
}
longestStr = longestStr.substring(repeatIndex + 1) + tmpChar;
currentLength = longestStr.length();
} else {
longestStr += s.charAt(i);
currentLength++;
}
} if(longestLength < currentLength) {
return currentLength;
} return longestLength;
}
}

提交后执行效率只击败了15.65%

为提高效率,考虑更新如下方面:

1. String 用StringBuilder替换(不需要线程安全,所以不使用开销大的StringBuffer)

2. char用String替换(是为了避免循环中char到String转换时的开销)

修改后的代码如下:

class Solution {
public int lengthOfLongestSubstring(String s) {
int longestLength = 0;
int currentLength = 0;
StringBuilder builder = new StringBuilder();
String charStr;
int repeatIndex; for(int i = 0; i < s.length(); i++){
charStr = s.substring(i, i + 1);
repeatIndex = builder.indexOf(charStr, 0);
// 遇到重复,从重复的下一位开始
if(repeatIndex >= 0) {
if(longestLength < currentLength){
longestLength = currentLength;
}
builder.delete(0, repeatIndex + 1);
builder.append(charStr);
currentLength = builder.length();
} else {
builder.append(charStr);
currentLength++;
}
} if(longestLength < currentLength) {
return currentLength;
} return longestLength;
}
}

提交后执行效率只击败了65.57%

如何再进一步提高效率呢?因为循环里面有查询字符存在的功能,考虑替换数据结构,用HashMap替换StringBuffer,

但是在思考了一会没有找到使用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;
}
}

其实实现的逻辑跟之前的思路一致,j 作为整个字符串的额游标,i 作为遇到重复元素后重新开始位置的游标,ans记录最大值

这样的写法不但是用HashMap来代替String查找,而且不用单独存储子字符串。

复杂度如下:

Complexity Analysis

  • Time complexity : O(n)O(n). Index jj will iterate nn times.

  • Space complexity (HashMap) : O(min(m, n)).

参考:

https://leetcode.com/problems/longest-substring-without-repeating-characters

LeeCode(No3 - Longest Substring Without Repeating Characters)的更多相关文章

  1. 最长子串(Leetcode-3 Longest Substring Without Repeating Characters)

    Question: Given a string, find the length of the longest substring without repeating characters. Exa ...

  2. LeeCode Algorithm #3 Longest Substring Without Repeating Characters

    一开始以为是不连续的,其实要求子串是连续的.想法:two-pointer O(n)时间,再借助256大小的int数组.两个下标i,j(i<=j).对于i=0,找到最右侧的字符不重复的下标的后一位 ...

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

    题目描述 给定一个字符串,找出不含有重复字符的最长子串的长度. 示例: 给定 "abcabcbb" ,没有重复字符的最长子串是 "abc" ,那么长度就是3. ...

  4. LeetCode:Longest Substring Without Repeating Characters(最长不重复子串)

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

  5. Longest Substring Without Repeating Characters(Difficulty: Medium)

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

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

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

  7. 【leetcode】Longest Substring Without Repeating Characters (middle)

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  8. 3. Longest Substring Without Repeating Characters (ASCII码128个,建立哈西表)

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  9. LeetCode OJ:Longest Substring Without Repeating Characters(最长无重复字符子串)

    Given a string, find the length of the longest substring without repeating characters. For example, ...

随机推荐

  1. Struts2 结合 Freemarker 实例

     Freemarker 是一个不依赖 web 容器的模板引擎,一个基于模板生成文本输出的工具.其工作的原理如下图: freemarker 不是一个 web 应用的框架,而适合作为 web 应用的一个组 ...

  2. MySql 5.7中添加用户,新建数据库,用户授权,删除用户,修改密码

    转自http://blog.csdn.net/w690333243/article/details/76576952 1.新建用户 创建test用户,密码是1234. MySQL -u root -p ...

  3. 基于R语言的RRT算法效率统计

  4. STM32 C++编程 005 I2c(Soft)类

    使用 C++ 语言给 STM32 编写一个 I2c(Soft)类 我使用的STM32芯片:STM32F103ZET6 我们使用的STM32库版本:V3.5.0 注意: 想学习本套 STM32 C++编 ...

  5. Day Day Up—— ——fseek()函数的用法

    在牛客网遇到的一个程序题中用到了函数fseek()故查阅了一下该函数的功能及用法,整理如下: fseek函数功能是把文件指针指向文件的开头,需要包含头文件stdio.h 功 能: 重定位流上的文件指针 ...

  6. Spring第五篇

    在Spring第四篇中 我们主要介绍了set get的注入方式 在Spring第五篇中 我们主要介绍使用注解配置Spring 主要分为两个步骤 1 导包的同时引入新得约束 导包如下 1.1 重写注解代 ...

  7. CodeForces 703C Chris and Road (简单几何)

    题意:有一个n边形的汽车向以速度v向x轴负方向移动,给出零时时其n个点的坐标.并且有一个人在(0,0)点,可以以最大速度u通过w宽的马路,到达(0,w)点.现在要求人不能碰到汽车,人可以自己调节速度. ...

  8. C++新标准:列表初始化

    一.列表初始化意义 C++新标准为vector提供了一种新的初始化方式:列表初始化.适用于知道多个成员具体值的情况. 二.列表初始化用法 /*1.空vector<int>*/ vector ...

  9. HTTP 协议 -- 基础

    概述 HTTP 是基于 TCP/IP 协议的应用层协议,它不涉及数据包(packet)的传输,主要是规定客户端和服务器之间的通信格式,默认使用 80 端口.   HTTP 协议最早版本是 HTTP/0 ...

  10. Vistual Studio的导出模板功能

    应用场景,每个项目有自己固定的目录结构和引用文件, 无需每次创建一个项目,就手工一一将那些目录再new一遍.如图所示 菜单  文件=>导出模板 之后的操作基本上一路"下一步" ...