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位。比较当前不重复字符串长度与max值,更新max值。最后返回max值。

字符的记录可用hashmap或int[256], 记录和查寻字符的方式有两种:

第一种记录每个字符上次出现的位置,当遇到新字符时,直接记录字符位置。当遇到的字符存在时,如果记录的位置小于左指针的位置,说明被跳过了,重新记录当前位置。当遇到重复的字符存在并且在左指针右边(在窗口内)时,指针直接跳到记录的这个字符上次出现的位置加1,就是跳过这个字符,然后记录这个新的位置。

第二种记录字符是否出现,如果扫到重复的字符,左指针向右移动,到重复字符的位置加1,这之间扫过的字符都删掉,因为不在窗口里了。

Java:

  public int lengthOfLongestSubstring(String s) {
if (s.length()==0) return 0;
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
int max=0;
for (int i=0, j=0; i<s.length(); ++i){
if (map.containsKey(s.charAt(i))){
j = Math.max(j,map.get(s.charAt(i))+1);
}
map.put(s.charAt(i),i);
max = Math.max(max,i-j+1);
}
return max;
}  

Java: HashMap

public class Solution {
public int lengthOfLongestSubstring(String s) {
HashMap<Character, Integer> map = new HashMap<Character,Integer>();
int maxLen = 0;
int start = 0;
for (int i = 0; i< s.length(); i++) {
if (!map.containsKey(s.charAt(i)) || map.get(s.charAt(i)) < start) {
map.put(s.charAt(i),i);
}
else {
maxLen = Math.max(maxLen,i - start);
start = map.get(s.charAt(i)) + 1;
map.put(s.charAt(i),i);
}
} maxLen = Math.max(maxLen, s.length() - start); return maxLen;
}
}

Java 2: HashSet

class Solution {
public int lengthOfLongestSubstring(String s) {
int longest = 0;
int i = 0;
Set<Character> set = new HashSet<>(); for (int j = 0; j < s.length(); j++) {
while (set.contains(s.charAt(j))) {
set.remove(s.charAt(i));
i++;
} set.add(s.charAt(j));
longest = Math.max(longest, j - i + 1);
}
return longest;
}
}

Java 3: boolean[256]

public class Solution {
public int lengthOfLongestSubstring(String s) {
boolean[] visited = new boolean[256];
int start = 0, last = 0, max = 0, length = s.length();
while(last < length) {
if(!visited[s.charAt(last)]) {
visited[s.charAt(last)] = true;
max = Math.max(max, last++ - start + 1);
} else {
while(visited[s.charAt(last)])
visited[s.charAt(start++)] = false;
visited[s.charAt(last++)] = true;
}
}
return max;
}
}

Java 4: int[256]

public class Solution {
public int lengthOfLongestSubstring(String s) {
int[] m = new int[256];
Arrays.fill(m, -1);
int res = 0, left = -1;
for (int i = 0; i < s.length(); ++i) {
left = Math.max(left, m[s.charAt(i)]);
m[s.charAt(i)] = i;
res = Math.max(res, i - left);
}
return res;
}
}

Python:

class Solution2(object):
def lengthOfLongestSubstring(self, s):
a={}
count=0
first=-1
for i in range(len(s)):
if s[i] in a and a[s[i]]>first:
first=a[s[i]]
a[s[i]]=i
count=max(count,(i-first))
return count

Python:

class Solution(object):
def lengthOfLongestSubstring(self, s):
a=[-1]*256
count=0
first=-1
for i in range(len(s)):
if a[ord(s[i])]>first:
first=a[ord(s[i])]
a[ord(s[i])]=i
count=max(count,(i-first))
return count

Python:

class Solution:
def lengthOfLongestSubstring(self, s):
longest, start, visited = 0, 0, [False for _ in xrange(256)]
for i, char in enumerate(s):
if visited[ord(char)]:
while char != s[start]:
visited[ord(s[start])] = False
start += 1
start += 1
else:
visited[ord(char)] = True
longest = max(longest, i - start + 1)
return longest

Python: wo

class Solution():
def longestSubstring(self, s):
longest, lookup = 0, {}
for k, v in enumerate(s):
if v not in lookup:
lookup[v] = k
longest = max(longest, len(lookup))
else:
for j in range(lookup[v], k):
lookup.pop(s[j]) return longest  

Python: wo

class Solution(object):
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
start, mx, m = 0, 0, {}
for i in xrange(len(s)):
if s[i] in m and m[s[i]] >= start:
start = m[s[i]] + 1
m[s[i]] = i
mx = max(mx, i - start + 1) return mx 

C++:

class Solution {
public:
int lengthOfLongestSubstring(string s) {
int m[256] = {0}, res = 0, left = 0;
for (int i = 0; i < s.size(); ++i) {
if (m[s[i]] == 0 || m[s[i]] < left) {
res = max(res, i - left + 1);
} else {
left = m[s[i]];
}
m[s[i]] = i + 1;
}
return res;
}
}; 

C++:

class Solution {
public:
int lengthOfLongestSubstring(string s) {
vector<int> m(256, -1);
int res = 0, left = -1;
for (int i = 0; i < s.size(); ++i) {
left = max(left, m[s[i]]);
m[s[i]] = i;
res = max(res, i - left);
}
return res;
}
};

    

类似题目:

[LeetCode] 76. Minimum Window Substring 最小窗口子串

[LeetCode] 159. Longest Substring with At Most Two Distinct Characters 最多有两个不同字符的最长子串

[LeetCode] 340. Longest Substring with At Most K Distinct Characters 最多有K个不同字符的最长子串

[LeetCode] 727. Minimum Window Subsequence 最小窗口子序列  

All LeetCode Questions List 题目汇总

 

[LeetCode] 3.Longest Substring Without Repeating Characters 最长无重复子串的更多相关文章

  1. leetcode 3 Longest Substring Without Repeating Characters最长无重复子串

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

  2. [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串

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

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

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

  4. [LeetCode] Longest Substring Without Repeating Characters最长无重复子串

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

  5. [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串

    Given a string, find the length of the longest substring without repeating characters. Example 1: In ...

  6. [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串 C++实现java实现

    最长无重复字符的子串 Given a string, find the length of the longest substring without repeating characters. Ex ...

  7. 【LeetCode每天一题】Longest Substring Without Repeating Characters(最长无重复的字串)

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

  8. LeetCode Longest Substring Without Repeating Characters 最长不重复子串

    题意:给一字符串,求一个子串的长度,该子串满足所有字符都不重复.字符可能包含标点之类的,不仅仅是字母.按ASCII码算,就有2^8=128个. 思路:从左到右扫每个字符,判断该字符距离上一次出现的距离 ...

  9. 003 Longest Substring Without Repeating Characters 最长不重复子串

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

随机推荐

  1. vue cli 框架搭建

    =============== 通知: 博主已迁至<掘金>码字,博客园可能以后不再更新,掘金地址:https://juejin.im/post/5a1a6a6551882534af25a8 ...

  2. Gym-100648B: Hie with the Pie(状态DP)

    题意:外卖员开始在0号节点,有N个人点了外卖,(N<=10),现在告诉两两间距离,问怎么配送,使得每个人的外卖都送外,然后回到0号点的总时间最短,注意,同一个点可以多次经过. 思路:TSP问题( ...

  3. Non-boring sequences(启发式分治)

    题意:一个序列被称作是不无聊的,当且仅当,任意一个连续子区间,存在一个数字只出现了一次,问给定序列是否是不无聊的. 思路:每次找到一个只出现了一次的点,其位置的pos,那么继续分治[L,pos-1], ...

  4. shell脚本中向hive动态分区插入数据

    在hive上建表与普通分区表创建方法一样: CREATE TABLE `dwa_m_user_association_circle`( `device_number` string, `oppo_nu ...

  5. 服务端高并发分布式架构演进之路 转载,原文地址:https://segmentfault.com/a/1190000018626163

    1. 概述 本文以淘宝作为例子,介绍从一百个到千万级并发情况下服务端的架构的演进过程,同时列举出每个演进阶段会遇到的相关技术,让大家对架构的演进有一个整体的认知,文章最后汇总了一些架构设计的原则. 特 ...

  6. Java线程池(ExecutorService)使用

    一.前提 /** * 线程运行demo,运行时打出线程id以及传入线程中参数 */ public class ThreadRunner implements Runnable { private fi ...

  7. 目标检测中的bounding box regression

    目标检测中的bounding box regression 理解:与传统算法的最大不同就是并不是去滑窗检测,而是生成了一些候选区域与GT做回归.

  8. 09-Flutter移动电商实战-移动商城数据请求实战

    1.URL接口管理文件建立 第一步需要在建立一个URL的管理文件,因为课程的接口会一直进行变化,所以单独拿出来会非常方便变化接口.当然工作中的URL管理也是需要这样配置的,以为我们会不断的切换好几个服 ...

  9. 改变Ubuntu命令行 用户名显示前缀

    改变Ubuntu命令行 用户名显示前缀 1.修改命令 [root@daokr ubuntu]#vim ~/.bashrc 修改第 56行 注释掉原来 # PS1='${debian_chroot:+( ...

  10. 61、Spark Streaming:部署、升级和监控应用程序

    一.部署应用程序 1.流程 1.有一个集群资源管理器,比如standalone模式下的Spark集群,Yarn模式下的Yarn集群等. 2.打包应用程序为一个jar包. 3.为executor配置充足 ...