LeetCode:Longest Substring Without Repeating Characters

原创 2016年05月30日 20:47:59

Longest Substring Without Repeating Characters




Total Accepted: 152787 Total Submissions: 687012 Difficulty: Medium

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.

Subscribe to see which companies asked this question





















思路:

维护一个HashMap存储每个字符的位置;两个指针i,j,维护最长子串的范围。


java code:

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


版权声明:本文为博主原创文章,转载请注明出处。

【LeetCode-面试算法经典-Java实现】【003-Longest Substring Without Repeating Characters(最长非重复子字符串)】

【003-Longest Substring Without Repeating Characters(最长非重复子字符串)】给定一个字符串,找字符中的最大非重复子串用start记录当前处理的开始位置...

【LeetCode】Longest Substring Without Repeating Characters

问题描述Given a string, find the length of the longest substring without repeating characters.Examples:G...

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

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

Longest Substring Without Repeating Characters-----LeetCode

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

leetcode003 Longest Substring Without Repeating Characters

题目3. Longest Substring Without Repeating Characters Given a string, find the length of the longest ...

leetcode: Longest Substring Without Repeating Characters

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

leetcode4 ---Longest Substring Without Repeating Characters

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

(leetcode) Longest Substring Without Repeating Characters

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

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:Longest Substring Without Repeating Characters
举报原因:
原因补充:

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