using System; using System.Collections.Generic; class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param arr int整型一维数组 the array * @return int整型 */ public int maxLength (List<int> arr) { // write code here int start = 0; int end = 0; int max = 1; Dictionary<int,int> sets=new Dictionary<int, int>(); while (start < arr.Count) { if (sets.ContainsKey(arr[start])) { end = Math.Max(end, (int)sets[arr[start]]+1); } max = Math.Max(max, start - end+1); sets[arr[start]]=start; start++; } return max; } }双指针不必多说,关键至于更新时怎么避免和前面重复,你可以用hash表。当更新时,和hash表中的某个值重复时,要做两件事情:1 将尾指针移到重复元素的下一位;2.更新该元素的位置(用最新的位置)。每回合更新长度,这样保证便利下来找到最长的