【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 substring is "b", with the length of 1.

【思路】

一開始我是用 HashMap 做的,然后就在考虑 字母做key还是下标做key,总之比較麻烦。

后来看到网上普遍用数组来实现 HashMap 的功能。感觉还是挺新的。

至于为什么数组的大小设为256,始终想不明确。我查了下ASCII码表,上面也仅仅有127个字符。A~Z是65~90,a~z是97~112。

以下的代码我认为是已经相当简洁了。參考出处:JavaC++。大家共同学习。

public class Solution {
    public int lengthOfLongestSubstring(String s) {
        int len = s.length();
        if (s == null || len == 0) return 0;

        int[] table = new int[256];
        Arrays.fill(table, -1);

        int maxlen = 1;
        int begin = 0, end = 1;
        table[s.charAt(0)] = 0; //important!
        while (end < len) {
            char endch = s.charAt(end);
            if (table[endch] >= begin) {
                begin = table[endch] + 1;
            }
            table[endch] = end;
            maxlen = Math.max(maxlen, end-begin+1);
            end++;
        }

        return maxlen;
    }
}

上面代码有下面优点:它推断有没有反复是通过当前字母在table中的值是不是比子串的開始位置大,这样就不用每次出现反复字母后都须要把之前的字符在table中的值又一次设为-1。

时间: 2024-08-10 21:28:59

【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. Examples: Given "abcabcbb", the answer is "abc", which the length is 3. Given "bbbbb", the answer is "b", with the length of 1.

Longest Substring Without Repeating Characters解题报告

使用双指针,i遍历全部字符,start收集重复的次数,最后不重复出现的字符个数maxx为i-start+1; //  main.cpp //  Longest Substring // //  Created by Bowie Hsu  on 14/11/21. //  Copyright (c) 2014年 Bowie Hsu . All rights reserved. // #include <iostream> #include <string> #include <

[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 题解

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 最长无重复子串

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 python

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