Longest Substring Without Repeating Characters Leetcode

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.

以后遇到string要首先想到hashmap啦,two pointers啦什么的。

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

这次做的还可以,自己测了test case后一遍就过了。吼吼吼,当然代码还是有冗余,又改良了一下

public class Solution {
    public int lengthOfLongestSubstring(String s) {
        if (s == null || s.length() == 0) {
            return 0;
        }
        int start = 0;
        int end = 0;
        int max = 0;
        Map<Character, Integer> hs = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            if (hs.containsKey(s.charAt(i)) && hs.get(s.charAt(i)) >= start) {
                max = Math.max(max, end - start);
                start = hs.get(s.charAt(i)) + 1;
            }
            hs.put(s.charAt(i), i);
            end++;
        }
        max = Math.max(max, end - start);
        return max;
    }
}
时间: 2024-10-29 10:46:37

Longest Substring Without Repeating Characters Leetcode的相关文章

Longest Substring Without Repeating Characters leetcode java

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

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

题目: 给定一个字符串,返回该串没有重复字符的最长子串. 分析: 1)子串:子串要求是连续的. 2)无重复,出现重复就断了,必须从新的位置开始.而新的位置就是重复字符第一次出现位置的下一个位置. 3)整个串可能没有一处重复. 那么,为了找出当前访问的字符是否出现过,要怎么做呢?当然是hash,O(1)的时间,而且既然是字符, 定义个255的hash table 就可以了,hash table 中的元素为相应字符在s中出现的位置.初始化为-1,表示都没有出现. 我们另外定义一个start 和end

[LeetCode][Python]Longest Substring Without Repeating Characters

# -*- coding: utf8 -*-'''__author__ = '[email protected]'https://oj.leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, find the length of the longest substring without repeating characters.For example, the longest s

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