从求数组Top N个数开始说起的求数组的若干问题笔记

来源:互联网 发布:scp linux 命令 编辑:程序博客网 时间:2024/05/16 06:58

随便百度看到有博客关于求数组的最大N个数的问题的文章,想起好像在编程之美上有这么一道题,好多天没有练练数据结构的题目,就从求数组的最大K个数说起。

关于这道题目编程之美提供了很多种算法,此处受插入排序的启发,提供一种改进的插入排序算法如下,最坏时间复杂度为O(n * m),空间复杂度为1.提供实现代码如下:

void GetMaxList(int[] m,int n){//此算法为插入排序的启发   if(m == null || m.length < n)    return ;   for(int i = 1;i<m.length;i++){   int j;   if(i < n){ //此处判断的目的一直保证前N个数字有序    j = i-1;   }else{     j = n-1;   }   int temp = m[i];   while(j >= 0 && m[j] < temp){    m[j+1] = m[j];    j--;   }   if(j != i-1)   m[j+1] = temp;   }   for(int i = 0;i<n;i++ ){   System.out.print(m[i] + "\t");   }}

以上解法自我感觉运用了点小技巧,通过插入排序的启发,此处对数组m中前n个元素进行插入排序,n个元素之后只需要维护前n个元素最大有序就行,则插入排序完成后,取前n个元素就是最大的n个元素。翻看编程之美的多种解法,最后百度的时候看到CSDN的v_JULY_v大神的一篇文章程序员编程艺术:第三章续、Top K算法问题的实现 对求数组的最大K个数的深入解读,深刻感受大牛到底厉害在哪里,大牛对待编程艺术的严谨和深入,在此,对v_JULY_v大神的系列博客膜拜一次。

求完数组的最大N个数后,我也来一次举一反三,那取数组的任意N个数情况该怎么处理,以下针对求数组{1,3,4,6,7,2}的任意3个数的所有组合给出自己不成熟的解答,此处的实现思路如下:

我们可以设定数组{1,3,4,6,7,2}取出的元素按数组的索引从小到大去取,因此,我们第一个数取1的话,取第二个数只能从{3,4,6,7,2} 取,取完第二个数,第三个数只能从剩下的去取。我们知道,我们取第一个数可以取{1,3,4,6},实际在取数组的时候对从索引0 到 数组长度 length -n +1取。

Q2:求数组{1,3,4,6,7,2}的3个数的所有组合, 代码实现如下:

public static void main(String[] args) {    int[] numArr = {1,3,4,6,7,2};    int n = 3;    getMaxNList(numArr,n);}static void getMaxNList(int[] numArr,int n){ int[] result = new int[n]; for(int i =0;i< numArr.length - n+1;i++){ result[0] = numArr[i];     for(int j = i+1;j<numArr.length - n +2;j++){     result[1] = numArr[j];     for(int k = j+1;k<numArr.length-n+3;k++){     result[2] = numArr[k];     print(result);     }      }  }}static void print(int[] result){for(int num : result){System.out.print(num);System.out.print(" ");}System.out.println();}
输出结果如下:

1 3 4 1 3 6 1 3 7 1 3 2 1 4 6 1 4 7 1 4 2 1 6 7 1 6 2 1 7 2 3 4 6 3 4 7 3 4 2 3 6 7 3 6 2 3 7 2 4 6 7 4 6 2 4 7 2 6 7 2

针对以上解法,我们发现当我们随机取的N比较大的时候,我们的for循环有N此之多,可知以上算法不可取,针对每次循环的规律和for,我想大家会马上想到,for循环改成递归调用,代码量就会小不少。

Q2改进: 递归求数组的任意N个数的所有组合,代码实现如下:

public static void main(String[] args) {    int[] numArr = {1,3,4,6,7,2};    int n = 3;    getMaxNList(numArr,n);}static void  getMaxNList(int[] numArr,int n){int[] result = new int[n];loopMaxNList(numArr,0,0,n,result);}static void loopMaxNList(int[] numArr,int startIndex,int curIndex,final int num,int[] result){ if(curIndex < num ){ //递归结束条件,当前result给元素赋值索引等于result的长度 for(int i = startIndex;i < numArr.length - num + curIndex + 1;i++){   result[curIndex] = numArr[i];   loopMaxNList(numArr,i+1,curIndex+1,num,result);   } }else{ //最内层递归结束时,读出所有元素 print(result); }}static void print(int[] result){for(int num : result){System.out.print(num);System.out.print(" ");}System.out.println();}

以上求完数组{1,3,4,6,7,2}的3个数的实现,我们再来举一反三,再来变化下题目的条件,我们来求n个数组任意选取一个元素的所有组合,针对以上的递归解法,我们发现求n个数组任意选取一个元素的所有组合变的很简单,实现思路如下:

我们递归的最外层,我们取第一个数组,我们可以循环遍历取任何一个元素,然后我们递归时候 取第二个数组,我们再次循环遍历取任意一个元素,依次循环下去,当我们循环取到数组集合的最后一个数组时,我们的最内层循环结束。此时,读出我们每一层循环的数组就是所有的情况。以下给出代码实现如下:

Q3: n个数组任意选取一个元素的所有组合,代码实现如下:

public static void main(String[] args) {List<int[]> arrList= new ArrayList<int[]>();        arrList.add(new int[]{1,5,9});        arrList.add(new int[]{2,4});        arrList.add(new int[]{6,8,3});        getResult(arrList);}static void  getResult(List<int[]> arrList){int n = arrList.size();int[] result = new int[n];    getNum(arrList,result,n,0);}static void getNum(List<int[]> arrList,int[] result,int n,int curIndex){if(curIndex < n){ //递归结束条件,当前已经在每一个数组都已经取了一个元素int[] arr = arrList.get(curIndex);for(int i = 0;i<arr.length;i++){ result[curIndex] = arr[i]; getNum(arrList,result,n,curIndex+1);}}else{ //递归结束时候打印出数组print(result);}}static void print(int[] result){for(int num : result){System.out.print(num+"\t");}System.out.println();}
我们可知我们的输出所有组合应该是3*2*3 = 18种。我们的输出如下:

126128123146148143526528523546548543926928923946948943









1 0
原创粉丝点击