3. Longest Substring Without Repeating Characters

转载 2016年06月01日 14:37:04

题目:

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

Examples:

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.


题意:

给定一个字符串,找到不包含重复元素的最长子串,返回最长子串的长度值;


思路:

输入的字符串都是字母,则新建一个数组,利用字母的ASCII码作为下标,将该字母出现的位置存储在数组中,下一次出现该字符时,下标作差即可求出重复元素之间的长度。

代码:C++版16ms

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        
        vector<int> charIndex(256, -1);
        int longest = 0, lastlen = 0;
        
        for(int i=0; i<s.size(); i++){
            lastlen = max(charIndex[s[i]]+1, lastlen);  //上一次出现当前元素的下标
            charIndex[s[i]] = i; //将当前元素出现的位置保存到数组中
            longest = max(longest, i-lastlen+1); 
        }
        
        return longest;
    }
};
java版:20ms

public class Solution {
    public int lengthOfLongestSubstring(String s) {
        
        if(s.length()==0) return 0;
        HashMap<Character, Integer> map = new HashMap<Character, Integer>();
        int longest = 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);
            longest = Math.max(longest, i-j+1);
        }
        return longest;
    }
}
思路二:

使用set来保存不相同的最长子串,有不同的元素,则加入set集合中,否则移除set中的一个元素。

代码:java版:26ms

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


相关文章推荐

[LeetCode - 滑动窗口] 3. Longest Substring Without Repeating Characters

1 问题 Given a string, find the length of the longest substring without repeating characters. Exa...

LeetCode- 3. Longest Substring Without Repeating Characters

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

leetcode_3 - Longest Substring Without Repeating Characters

leetcode_3 : Longest Substring Without Repeating Characters标签(空格分隔): leetcode string twoPoints setGi...

LeetCode(3)--Longest Substring Without Repeating Characters

题目如下: Given a string, find the length of the longest substring without repeating characters. For ex...

LeetCode 3. Longest Substring Without Repeating Characters(线性处理, 哈希)

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 e...

LeetCode#3. Longest Substring Without Repeating Characters

求最长子串(子串中没有重复元素)

LeetCode3. Longest Substring Without Repeating Characters(最长不重复子串)

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

leetcode 3 -- Longest Substring Without Repeating Characters

leetcode 3 – Longest Substring Without Repeating Characters 题目: Given a string, find the lengt...

LeetCode题解(Week 16):3. Longest Substring Without Repeating Characters

原题目:Given a string, find the length of the longest substring without repeating characters. Example...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:3. Longest Substring Without Repeating Characters
举报原因:
原因补充:

(最多只允许输入30个字)