成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專(zhuān)欄INFORMATION COLUMN

[LintCode] Longest Substring Without Repeating Cha

Scliang / 2647人閱讀

摘要:哈希表法雙指針?lè)ㄖ挥挟?dāng)也就是時(shí)上面的循環(huán)才會(huì)結(jié)束,,跳過(guò)這個(gè)之前的重復(fù)字符再將置為

Problem

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

Example

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.

Note Solution

1. 哈希表法

HashMap Method I

  public class Solution {
    public int lengthOfLongestSubstring(String s) {
        HashMap map = new HashMap();
        int count = 0, start = 0;
        for (int i = 0; i < s.length(); i++) {
            if (map.containsKey(s.charAt(i))) {
                int index = map.get(s.charAt(i));
                for (int j = start; j <= index; j++) {
                    map.remove(s.charAt(j));
                }
                start = index + 1;
            }
            map.put(s.charAt(i), i);
            count = Math.max(count, i - start + 1);
        }
        return count;
    }
}

HashMap Method II

public class Solution {
    public int lengthOfLongestSubstring(String s) {
        if (s == null || s.length() == 0) return 0;
        HashMap map = new HashMap<>();
        int max = 1, start = 0;
        for (int i = 0; i < s.length(); i++) {
            Character cur = s.charAt(i);
            Integer rep = map.get(cur);
            if (rep != null && rep >= start) {
                max = Math.max(max, i-start);
                start = rep+1;
            }
            map.put(cur, i);
        }
        return Math.max(s.length()-start, max);
    }
}

2. 雙指針?lè)?/strong>

public class Solution {
    public int lengthOfLongestSubstring(String s) {
        int start = 0, end = 0, count = 0;
        boolean[] mark = new boolean[256];
        while (end < s.length()) {
            if (!mark[s.charAt(end)]) {
                mark[s.charAt(end)] = true;
                count = Math.max(count, end-start+1);
                end++;
            }
            else {
                while (mark[s.charAt(end)]) {
                    mark[s.charAt(start)] = false;
                    start++;
                }
                
                //只有當(dāng)s.charAt(start) == s.charAt(end)
                //也就是mark[s.charAt(end)] == false時(shí)
                //上面的循環(huán)才會(huì)結(jié)束,start++,跳過(guò)這個(gè)之前的重復(fù)字符
                //再將mark[s.charAt(end)]置為true
                
                mark[s.charAt(end)] = true;
                end++;
            }
        }
        return count;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/65600.html

相關(guān)文章

  • [Leetcode] Longest Substring Without Repeating Cha

    摘要:哈希表是最自然的想法。在遍歷字符串時(shí),我們先根據(jù)哈希表找出該字符上次出現(xiàn)的位置,如果大于等于子字符串首,便更新子字符串首。結(jié)束后,將該字符新的位置放入哈希表中。 Longest Substring Without Repeating Characters 最新更新解法:https://yanjia.me/zh/2018/12/... Given a string, find the ...

    FleyX 評(píng)論0 收藏0
  • [LeetCode] Longest Substring Without Repeating Cha

    Problem 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...

    graf 評(píng)論0 收藏0
  • [LeetCode] Longest Substring Without Repeating Cha

    摘要:建立數(shù)組,存儲(chǔ)個(gè)字符最近一次出現(xiàn)的位置。首次出現(xiàn)某字符時(shí),其位置標(biāo)記為,并用無(wú)重復(fù)字符計(jì)數(shù)器記錄無(wú)重復(fù)字符的長(zhǎng)度,再在更新其最大值。循環(huán)完整個(gè)字符串后,返回最大值。 Problem Given a string, find the length of the longest substring without repeating characters. Examples: Given ...

    CoderStudy 評(píng)論0 收藏0
  • leetcode 3 Longest Substring Without Repeating Cha

    摘要:題目詳情題目要求輸入一個(gè)字符串,我們要找出其中不含重復(fù)字符的最長(zhǎng)子字符串,返回這個(gè)最長(zhǎng)子字符串的長(zhǎng)度。對(duì)于字符串中的每一個(gè)字符,先判斷中是否已經(jīng)存在這個(gè)字符,如果不存在,直接將添加到,如果已存在,則新的字符串就從不含前一個(gè)字符的地方開(kāi)始。 題目詳情 Given a string, find the length of the longest substring without repe...

    xcold 評(píng)論0 收藏0
  • [Leetcode]Longest Substring Without Repeating Char

    摘要:解題思路本題借助實(shí)現(xiàn)。如果字符未出現(xiàn)過(guò),則字符,如果字符出現(xiàn)過(guò),則維護(hù)上次出現(xiàn)的遍歷的起始點(diǎn)。注意點(diǎn)每次都要更新字符的位置最后返回時(shí),一定要考慮到從到字符串末尾都沒(méi)有遇到重復(fù)字符的情況,所欲需要比較下和的大小。 Longest Substring Without Repeating CharactersGiven a string, find the length of the lon...

    awesome23 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<