找到具有不同整数的最长子阵列

时间:2016-03-09 03:30:16

标签: java arrays algorithm

编写一个采用整数数组的方法,并返回具有不同整数的最长子数组的长度。

e.g。使用 <ScrollView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/scrollView" > <FrameLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/wrong_questions_result" android:layout_width="match_parent" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="Wrong Questions" android:paddingTop="10dp"/> <RadioGroup android:id="@+id/radioGroup1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0.04" > <RadioButton android:id="@+id/radio0" android:layout_width="match_parent" android:layout_height="wrap_content" android:checked="true" android:layout_marginTop="30dp" android:text="RadioButton" /> <RadioButton android:id="@+id/radio1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="RadioButton" /> <RadioButton android:id="@+id/radio2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="RadioButton" /> <RadioButton android:id="@+id/radio3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="RadioButton"/> </RadioGroup> </FrameLayout> </ScrollView> </LinearLayout> 时,应返回[1,2,3,4,2,3]

7 个答案:

答案 0 :(得分:4)

我使用HashSet来跟踪从索引i到索引j的所有元素,并在遍历列表时保留一个计数器。线性运行时和空间:

4

答案 1 :(得分:0)

public int[] getLargestSubArray(int[] array) {
    int length = array.length;
    Set<Integer> set = new HashSet<>();
    int longest = 0;
    int start = 0;

    int longestCurrent = 0;
    int startCurrent = 0;

    int j = 0;
    while (j < length) {
        if (!set.contains(array[j])) {
            set.add(array[j]);
            longestCurrent++;

            if (longestCurrent > longest) {
                longest = longestCurrent;
                start = startCurrent;
            }
            j++;
        } else {
            while (startCurrent < j) {
                longestCurrent--;
                if (array[startCurrent++] == (array[j])) {
                    break;
                }
            }
            set.remove(array[j]);
        }
    }
    int[] longestSubSequence = new int[longest];
    System.arraycopy(array, start, longestSubSequence, 0, longest);
    return longestSubSequence;
}

答案 2 :(得分:0)

public static int sizeOfLongestDistinctSubArrayO1(int[] arr) {
    int max = 0;
    Map<Integer, Integer> counts = new HashMap<>();
    int cur = 0;
    int prev = 0;
    for (int i = 0, len = arr.length; i < len; i++) {
        if (counts.containsKey(arr[i])) {
            int j = counts.get(arr[i]);
            max = Math.max(max, cur);
            prev = Math.max(j, prev);
            cur = i - prev;
            counts.put(arr[i], i);
        } else {
            cur++;
            counts.put(arr[i], i);
        }
    }
    return Math.max(max, cur);
}

答案 3 :(得分:0)

python版本

#!/usr/bin/env python3

input = [1,2,3,4,2,3,4,5,6,7,3,4]
tmp_list = []
max = 0

for i in input:
    if i not in tmp_list:
        tmp_list.append(i)
    else:
        max = len(tmp_list) if len(tmp_list) > max else max
        tmp_list = [i]

print (max if max >= len(tmp_list) else len(tmp_list))

答案 4 :(得分:0)

public int lengthOfLongestSubarray(int[] arr) {
        
    int max = 0; // Maximum length of subarray with unique elements
    Map<Integer,Integer> map = new HashMap<>();
    // let longest subarray starts at i and ends at j
    for(int i = -1,j = 0; j < arr.length;j++) {
        if(map.containsKey(arr[j])) {
            i = Math.max(i,map.get(arr[j]));
        }
        max = Math.max(max,j-i);
        map.put(arr[j],j);
    }
    return max;
}

答案 5 :(得分:0)

C#版本

核心逻辑:

private static int Solve(string s) {
    var dict = new Dictionary < string,int > ();
    var arr = s.Split(" ", StringSplitOptions.RemoveEmptyEntries);
    var start = 0;
    var substrLen = 0;
    for (var i = 0; i < arr.Length; i++) {
        if (dict.ContainsKey(arr[i])) {
            substrLen = substrLen < i - start ? i - start: substrLen;
            var tempStart = dict[arr[i]] + 1;
            for (var j = dict[arr[i]]; j > start; j--) {
                dict.Remove(arr[i]);
            }

            start = tempStart;
            dict[arr[i]] = i;
        }
        else {
            dict.Add(arr[i], i);
        }
    }

    return dict.Count;
}

完整的代码here

答案 6 :(得分:0)

import java.util.*; 
class GFG{ 
static int largest_subarray(int a[], int n) 
{ 
HashSet<Integer> set = new HashSet<Integer>(); 
int ans = 0; 
int counter = 0;
for(int i = 0; i < n; i++) 
{ 
  if(set.contains(a[i])){
     set.clear();
     counter =0;
     }
     set.add(a[i]);
     counter++;
     ans = Math.max(ans, counter);
     
   } 
// Return final ans 
return ans; 
} 

// Driver Code 
public static void main(String[] args) 
{ 
int arr[] = { 1, 2, 4, 4, 5, 6, 7, 8, 3, 4, 5, 3, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4 
}; 
int n = arr.length; 
System.out.print(largest_subarray(arr, n)); 
} 
}