Leet Code Longest Substring Without Repeating Characters

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.

给定字符串,输出最长不重复子序列。

算法:

双指针扫描

设置两个指针,start为满足串的初始化位置,cur为当前指针位置。

首先start 为 0,cur开始逐个扫描。如果扫描的元素flag为false表示为未出现过的,则标记其为true.

同时跟新最大长度串。

如果遇到的是true,则表示为扫描过的。则循环start并逐个重新标注为未扫描过的。

循环环整个串的元素后结束程序。

输出结果。

public class Solution {
    public int lengthOfLongestSubstring(String s) {
        boolean[] flag = new boolean[500];
        java.util.Arrays.fill(flag,false);
        int max = 0;
        for(int start=0,cur=0;cur<s.length();cur++)
        {
            while(flag[s.charAt(cur)]==true)
            {
                flag[s.charAt(start)] = false;
                start++;
            }
            flag[s.charAt(cur)]=true;
            max = Math.max(max,cur-start+1);
        }
        return max;
    }
}
时间: 2024-10-17 22:41:43

Leet Code Longest Substring Without Repeating Characters的相关文章

【Leet Code】Longest Substring Without Repeating Characters

Longest Substring Without Repeating Characters Total Accepted: 20506 Total Submissions: 92223My Submissions Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating le

[Leet code 3]Longest Substring Without Repeating Characters

1 题目 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

[C++]LeetCode: 105 Longest Substring Without Repeating Characters

题目: 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 s

LeetCode题解 || Longest Substring Without Repeating Characters (O(n)算法)问题

problem: 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 long

[Leetcode 3, Medium] Longest Substring Without Repeating Characters

Problem: 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 long

LeetCode第[3]题(Java):Longest Substring Without Repeating Characters 标签:Linked List

题目中文:没有重复字符的最长子串 题目难度: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 &q

leetcode4 ---Longest Substring Without Repeating Characters

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 subst

LeetCode: Longest Substring Without Repeating Characters 题解

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 subst

LeetCode3 Longest Substring Without Repeating Characters

题目: 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 s