LeetCode 3. Longest Substring Without Repeating Characters(java)

原创 2018年02月12日 02:08:43

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.
解题思路:首先我们假设走到i-1的位置时的最大subarray时的(start ~ i-1)中的start已经被维护,那么到了第i个位置,我们需要判断,第i个字符是否是重复字符,如果是,那么start移到start后面第一个相同字符的后面一个;如果不是,那么加进hashset,然后update那个max,最后走完返回max即可。
class Solution {
    public int lengthOfLongestSubstring(String s) {
        //special case
        if (s.length() <= 1) return s.length();
        //base case
        HashSet<Character> set = new HashSet<>();
        int start = 0, max = 0;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (set.contains(c)) {
                for (int j = start; j < i; j++) {
                    char cc = s.charAt(j);
                    if ( cc== c) {
                        start = j + 1;
                        break;
                    } else {
                        set.remove(cc);
                    }
                }
            } else {
                set.add(c);
                if (i - start + 1 > max) {
                    max = i - start + 1;
                }
            }
        }
        return max;
    }
}
看到以Character为key的hashmap就无脑用数组!!!因为Character的范围是固定的,所以可以直接用数组来代替。以下代码的效率更高。
 public int lengthOfLongestSubstring(String s) {
        //special case
        if (s.length() <= 1) return s.length();
        //base case
        int[] index = new int[256];
        int max = 0, start = 0;
        //由于0在这里可以是index = 0的意思,因此这里我们用-1来初始化index.
        for (int i = 0; i < 256; i++) {
            index[i] = -1;
        }
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (index[c] == -1) {
                index[c] = i;
                max = Math.max(max, i - start + 1);
            } else {
                for (int j = start; j < index[c]; j++) {
                    index[s.charAt(j)] = -1;
                }
                start = index[c] + 1;
                index[c] = i;
            }
        }
        return max;
    }

leetcode:3. Longest Substring Without Repeating Characters (java)

题目链接:https://leetcode.com/problems/longest-substring-without-repeating-characters/ 题目: Given a str...
  • j754379117
  • j754379117
  • 2016年06月20日 20:36
  • 857

Longest Substring Without Repeating Characters -- LeetCode

原题链接: http://oj.leetcode.com/problems/longest-substring-without-repeating-characters/  这道题用的方法是在Leet...
  • linhuanmars
  • linhuanmars
  • 2014年02月26日 01:53
  • 18844

LeetCode(3) Longest Substring Without Repeating Characters

LeetCode的第3题,给定一个字符串,找到其中的一个最长的字串,使得这个子串不包含重复的字符。 // // Solution.h // LeetCodeOJ_003_LongestUnique...
  • feliciafay
  • feliciafay
  • 2013年11月23日 04:25
  • 16847

LeetCode 3 — Longest Substring Without Repeating Characters (C++ Java Python)

题目:http://oj.leetcode.com/problems/longest-substring-without-repeating-characters/] Given a string, ...
  • dragon_dream
  • dragon_dream
  • 2014年02月18日 22:22
  • 4657

[LeetCode] 003. Longest Substring Without Repeating Characters (Medium) (C++/Java/Python)

[LeetCode] 003. Longest Substring Without Repeating Characters (Medium) (C++/Java/Python)
  • hcbbt
  • hcbbt
  • 2015年02月27日 12:55
  • 5792

LeetCode 3.Longest Substring Without Repeating Characters c语言版

问题描述: Given a string, find the length of the longest substring without repeating characters. Exa...
  • banana1006034246
  • banana1006034246
  • 2017年07月15日 17:35
  • 309

leetcode 3 Longest Substring Without Repeating Characters最长无重复子串

Given a string, find the length of the longest substring without repeating characters. For example, ...
  • wangyaninglm
  • wangyaninglm
  • 2016年04月05日 22:27
  • 2733

LeetCode 第三题,Longest Substring Without Repeating Characters

题目: Given a string, find the length of the longest substring without repeating characters. For examp...
  • hu1020935219
  • hu1020935219
  • 2014年08月03日 20:38
  • 7649

leetcode python - Longest Substring Without Repeating Characters

# Given a string, find the length of the longest substring without repeating characters. # # Example...
  • AMDS123
  • AMDS123
  • 2017年04月01日 15:07
  • 6338

Longest Substring Without Repeating Characters 最长不重复字符的字串 @LeetCode

Method 1 (Simple) We can consider all substrings one by one and check for each substring whether it ...
  • hellobinfeng
  • hellobinfeng
  • 2014年01月05日 01:56
  • 3311
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode 3. Longest Substring Without Repeating Characters(java)
举报原因:
原因补充:

(最多只允许输入30个字)