leetcode-algorithms-3 Longest Substring Without Repeating Characters

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

Example 1:

Input: "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.

Example 2:

Input: "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.

Example 3:

Input: "pwwkew"
Output: 3
Explanation: 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.

解法1

一步步检查所有字串看是否有重复的字符

class Solution
{
public:
int lengthOfLongestSubstring(string s)
{
string longest;
for (int i = 0; i < s.size(); ++i)
{
for (int j = s.size() - i; j > 0; --j)
{
string longesttemp = s.substr(i, j); bool repect = false;
int a[256] = {0};
for (int n = 0; n < longesttemp.size(); ++n)
{
int x = longesttemp[n];
++a[x];
if (a[x] > 1)
{
repect = true;
break;
}
}
if (!repect && (longesttemp.size() > longest.size())) longest = longesttemp;
}
}
return longest.size();
}
};

时间复杂度: O(n^3).

空间复杂度: O(min(n,m)).

解法2

解法1的时间复杂度太高了,效率低下.字符串查找要怎么提高效率,首先都应该想到Sliding Window算法.设定一个窗口[i, j],将i到j的内容存入map,下面滑动j,如果s[j] (s表示字符串)在map中存在,表示字符串已经重复了,记下最大值,然后将i窗口滑到s[j]上次的位置.

下面是代码的实现:

class Solution
{
public:
int lengthOfLongestSubstring(string s)
{
int n = s.length();
std::unordered_map<char, int> m;
int len = 0; for (int i = 0, j = 0; j < n; ++j)
{
auto fiter = m.find(s[j]);
if (fiter != m.end())
{
i = (fiter->second) > i ? fiter->second : i;
} int temp_len = j - i + 1;
len = (len > temp_len) ? len : temp_len;
m[s[j]] = j + 1;
}
return len;
}
};

时间复杂度: O(n).只有一个j循环.

空间复杂度: O(m).m是最大的不重复子串的长度.

解法3

对于解法2有没更快捷的方式.参考KMP算法对字符串的处理,我们可以将字符存入整型数组,值存储字符所在的位置,这样就可以在算法2的基础上少一次查询.

class Solution
{
public:
int lengthOfLongestSubstring(string s)
{
int n = s.length();
int len = 0;
int index[128] = {0}; for (int i = 0, j = 0; j < n; ++j)
{
i = i > index[s[j]] ? i : index[s[j]];
int temp_len = j - i + 1;
len = (len > temp_len) ? len : temp_len;
index[s[j]] = j + 1;
}
return len;
}
};

时间复杂度: O(n).

空间复杂度: O(128).

链接: leetcode-algorithms 目录

leetcode-algorithms-3 Longest Substring Without Repeating Characters的更多相关文章

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

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

  2. 【LeetCode OJ】Longest Substring Without Repeating Characters

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

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

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

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

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

  5. 【LeetCode】3. Longest Substring Without Repeating Characters

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

  6. leetcode题解 3. Longest Substring Without Repeating Characters

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

  7. 【LeetCode】3. Longest Substring Without Repeating Characters (2 solutions)

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

  8. 《LeetBook》leetcode题解(3):Longest Substring Without Repeating Characters[M]——哈希判断重复

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

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

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

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

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

随机推荐

  1. redis事务之watch

    三.redis事务之watch 首先要了解redis事务中watch的作用,watch命令可以监控一个或多个键,一旦其中有一个键被修改(或删除),之后的事务就不会执行.监控一直持续到exec命令(事务 ...

  2. TortoiseGit自动记住用户名密码的方法

    TortoiseGit自动记住用户名密码的方法 windows下比较比较好用的git客户端有2种: msysgit + TortoiseGit(乌龟git) GitHub for Windows gi ...

  3. #ifndef/#define/#endif使用详解

    问题: 想必很多人都看过"头文件中的 #ifndef/#define/#endif 防止该头文件被重复引用".但是是否能理解"被重复引用"是什么意思?是不能在不 ...

  4. JAVA之经典算法

    package Set.Java.algorithm; import java.util.Scanner; public class algorithm { /** * [程序1] 题目:古典问题:有 ...

  5. 《spring boot 实战》读书笔记

    前言:虽然已经用spring boot开发过一套系统,但是之前都是拿来主义,没有系统的,全面的了解过这套框架.现在通过学习<spring boot实战>这本书,希望温故知新.顺便实现自己的 ...

  6. scala中option、None、some对象

    转载:http://www.jianshu.com/p/95896d06a94d 1.option类型避免对象是空值,造成空指针异常. 2.None对象表示null,在没有对象返回时使用,some在有 ...

  7. 排序——选择排序(java描述)

    百度百科的描述如下:选择排序(Selection sort)是一种简单直观的排序算法.它的工作原理是每一次从待排序的数据元素中选出最小(或最大)的一个元素,存放在序列的起始位置,然后,再从剩余未排序元 ...

  8. IPC 之 AIDL 的使用

    一.AIDL 知识储备 1. AIDL 文件支持的数据类型: 基本数据类型 (int , long , char , boolean ,double 等): String 和 CharSequence ...

  9. 大话WebRTC的前世今生

    音视频的历史 音视频可以说是人类与生俱来的需求,人一出生就要用耳朵听,用眼睛看.中国的古代神话中为此还专门设置了两位神仙(千里眼和顺风耳),他们可以听到或看到千里之外的声音或景像. 为了解决听的远和看 ...

  10. 【C#】调用2.0踩过的坑

    1.初始化[DllImport(“libarcsoft_face_engine.dll”, EntryPoint = “ASFInitEngine”, CallingConvention = Call ...