LeetCode---------Longest Substring Without Repeating Characters解法

题目如下:



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.



大致翻译:



给出一个字符串,求出没有重复字符的最长子串的长度。

例如:

给出"abcabcbb",答案是"abc"的长度为3.

给出"bbbbb",答案是"b"的长度为1.

给出"pwwkew",答案是"wke"的长度为3.  注意答案必须是一个子串,"pwke"是一个子序列但并不是子串.



本题重点在于不重复的子串,想到HashSet是不允许存储重复的数据的,所以解法就利用HashSet来实现。

【Java代码】

public class Solution {
    public int lengthOfLongestSubstring(String s) {

        //不重复子串的长度
        int length = 0;

        //构造不重复的set表
        Set<Character> set = new HashSet<Character>();

        int i = 0, j = 0;

        for(i = 0; i < s.length(); i++){
            set.clear();//清空set表
            set.add(s.charAt(i));//加入开始字符
            for(j = i + 1; j < s.length(); j++){
                if(set.add(s.charAt(j)));//如果成功加入,证明没有重复,程序继续
                else break;//如果没成功加入,则跳出
            }
            if(j - i >= length) length = j - i;//计算长度并保留最长长度
        }

        return length;

    }
}

  

如果有任何问题,欢迎跟我联系:[email protected]

我的github地址:github.com/WXRain

时间: 2024-12-13 20:04:36

LeetCode---------Longest Substring Without Repeating Characters解法的相关文章

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

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

leetcode——Longest Substring Without Repeating Characters 求链表中无重复字符的最大字串长度(AC)

mnesia在频繁操作数据的过程可能会报错:** WARNING ** Mnesia is overloaded: {dump_log, write_threshold},可以看出,mnesia应该是过载了.这个警告在mnesia dump操作会发生这个问题,表类型为disc_only_copies .disc_copies都可能会发生. 如何重现这个问题,例子的场景是多个进程同时在不断地mnesia:dirty_write/2 mnesia过载分析 1.抛出警告是在mnesia 增加dump

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

[LeetCode] Longest Substring Without Repeating Characters [15]

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

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

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 Explana

[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

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

题意:给一字符串,求一个子串的长度,该子串满足所有字符都不重复.字符可能包含标点之类的,不仅仅是字母.按ASCII码算,就有2^8=128个. 思路:从左到右扫每个字符,判断该字符距离上一次出现的距离是多少,若大于max,则更新max.若小于,则不更新.每扫到一个字符就需要更新他的出现位置了.这里边还有个注意点,举例说明: 假如有长为16串 s="arbtbqwecpoiuyca" 当扫到第2个b时,距离上一个b的距离是2:(直接减) 当扫到第2个c时,距离上一个c的距离是6:(直接减

[Leetcode] Longest Substring Without Repeating Characters (C++)

题目: 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 解题报告

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.