Middle-题目114:3. Longest Substring Without Repeating Characters

原创 2016年05月31日 20:05:37

题目原文:
Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for “abcabcbb” is “abc”, which the length is 3. For “bbbbb” the longest substring is “b”, with the length of 1.
题目大意:
求一个字符串的没有重复字符的最长子串的长度。(不一定连续)
题目分析:
(算法思路来自度娘)定义一个布尔型数组sign[256],记录每个字符是否出现过。然后两个指针i和j都从头开始,先让j向右移动,直到第一次出现重复元素时,移动i至排除掉这个重复元素,再移动j,这样一直维护着以j为结尾的最长子串,所以能得到最优解。
源码:(language:java)

public class Solution {    
    public int lengthOfLongestSubstring(String s) {  
        int i,j,max=0;  
        boolean[] sign = new boolean[256];  
        for(i=0,j=0;j<s.length();j++) {              
            while(sign[s.charAt(j)])  
                sign[s.charAt(i++)] = false;  
            sign[s.charAt(j)] = true;              
            max=Math.max(max,j-i+1);  
        }  
        return max;  
    } 
}  

成绩:
5ms,beats 91.60%,众数18ms,8.16%
Cmershen的碎碎念:
该算法只需线性复杂度,既优雅又易于理解,值得学习!

版权声明:完整版Leetcode题解请出门左转https://github.com/cmershen1/leetcode/tree/master/docs

Longest Substring Without Repeating Characters题目及解法

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

[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...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Middle-题目114:3. Longest Substring Without Repeating Characters
举报原因:
原因补充:

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