#3 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.

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.

思路:
两个指针,移动第一个同时更新hashmap,如果字符已经在hashmap中存在,将第二个指针移动到这个字符的右侧
代码:

package solution;

import java.util.HashMap;

public class Solution3 {
    public static int lengthOfLongestSubstring(String s) {
        HashMap map = new HashMap();
        int max = 0;
        for(int i = 0, j = 0; i

你可能感兴趣的:(#3 Longest Substring Without Repeating Characters)