找到字符串的最长无重复字符子串

找到字符串的最长无重复字符子串

http://www.nowcoder.com/questionTerminal/b56799ebfd684fb394bd315e89324fb4

import java.util.*;


public class Solution {
    /**
     *
     * @param arr int整型一维数组 the array
     * @return int整型
     */
    public static int maxLength (int[] arr) {
        // write code here
        int maxLength = 0;
        LinkedList<Integer> list = new LinkedList<>();
        for(int i = 0 ; i < arr.length ; i++){
            while (list.contains(arr[i])){
                list.removeFirst();
            }
            list.add(arr[i]);
            if (list.size() > maxLength){
                maxLength = list.size();
            }
        }
        return maxLength;
    }

    public static void main(String[] args) {
        int[] arr = {2,2,3,4,3};
        System.out.println(maxLength(arr));
    }
}
全部评论

相关推荐

点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务