【LeetCode】3 Longest Substring Without Repeating Characters

原创 2015年07月07日 22:38:36
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.

Solution1

public class Solution {
    public int lengthOfLongestSubstring(String s) {
        boolean []exist = new boolean[256];
        int i=0,maxLen = 0;
        for(int j=0;j<s.length();j++)
        {
            while(exist[s.charAt(j)])
            {
                exist[s.charAt(i)] = false;
                i++;
            }
            exist[s.charAt(j)]=true;
            maxLen = Math.max(j-i+1, maxLen);
            System.out.println();
        }
        return maxLen;
    }  
}

Solution2 HashMap

public class Solution {
    public int lengthOfLongestSubstring(String s) {
        int maxLen = 0;
        int availableFrom = 0;
        Map<Character,Integer> map = new HashMap<Character,Integer>();
        for(int j=0;j<s.length();j++)
        {
            if(map.containsKey(s.charAt(j)))
            {
                int last = map.get(s.charAt(j));
                availableFrom = Math.max(availableFrom, last+1);
            }
            maxLen = Math.max(maxLen, j-availableFrom+1);
            map.put(s.charAt(j), j);
        }
        return maxLen;
    }  
}

相关文章推荐

[LeetCode - 滑动窗口] 3. Longest Substring Without Repeating Characters

1 问题 Given a string, find the length of the longest substring without repeating characters. Exa...

LeetCode- 3. Longest Substring Without Repeating Characters

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

leetcode_3 - Longest Substring Without Repeating Characters

leetcode_3 : Longest Substring Without Repeating Characters标签(空格分隔): leetcode string twoPoints setGi...

LeetCode(3)--Longest Substring Without Repeating Characters

题目如下: Given a string, find the length of the longest substring without repeating characters. For ex...

LeetCode 3. Longest Substring Without Repeating Characters(线性处理, 哈希)

LeetCode 3. Longest Substring Without Repeating Characters(线性处理, 哈希) 1. 从左到右扫描字符串,读入每一位字符。当该位字符上一次出现...

【leetcode】【3】Longest Substring Without Repeating Characters

一、问题描述 Given a string, find the length of the longest substring without repeating characters. For e...

LeetCode#3. Longest Substring Without Repeating Characters

求最长子串(子串中没有重复元素)

LeetCode3. Longest Substring Without Repeating Characters(最长不重复子串)

题目链接:https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a ...

leetcode 3 -- Longest Substring Without Repeating Characters

leetcode 3 – Longest Substring Without Repeating Characters 题目: Given a string, find the lengt...

LeetCode题解(Week 16):3. Longest Substring Without Repeating Characters

原题目:Given a string, find the length of the longest substring without repeating characters. Example...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:【LeetCode】3 Longest Substring Without Repeating Characters
举报原因:
原因补充:

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