美文网首页
2019-04-27.5-12 未完成

2019-04-27.5-12 未完成

作者: 张开翔 | 来源:发表于2019-05-12 23:42 被阅读0次

    question:

    In a deck of cards, every card has a unique integer. You can order the deck in any order you want.

    Initially, all the cards start face down (unrevealed) in one deck.

    Now, you do the following steps repeatedly, until all cards are revealed:

    Take the top card of the deck, reveal it, and take it out of the deck.
    If there are still cards in the deck, put the next top card of the deck at the bottom of the deck.
    If there are still unrevealed cards, go back to step 1. Otherwise, stop.
    Return an ordering of the deck that would reveal the cards in increasing order.

    The first entry in the answer is considered to be the top of the deck.
    Input: [17,13,11,2,3,5,7]
    Output: [2,13,3,11,5,17,7]

    Explanation:

    We get the deck in the order [17,13,11,2,3,5,7] (this order doesn't matter), and reorder it.
    After reordering, the deck starts as [2,13,3,11,5,17,7], where 2 is the top of the deck.
    We reveal 2, and move 13 to the bottom. The deck is now [3,11,5,17,7,13].
    We reveal 3, and move 11 to the bottom. The deck is now [5,17,7,13,11].
    We reveal 5, and move 17 to the bottom. The deck is now [7,13,11,17].
    We reveal 7, and move 13 to the bottom. The deck is now [11,17,13].
    We reveal 11, and move 17 to the bottom. The deck is now [13,17].
    We reveal 13, and move 17 to the bottom. The deck is now [17].
    We reveal 17.
    Since all the cards revealed are in increasing order, the answer is correct.

    ideas :

    There are two key points, the first is the sorting of the array, and the second is the order in which the cards are stored in the array.

    the sorting of the array

    1. Arrays.sort()

    2.Sorting method of writing by yourself
    这里复习下排序算法,尝试自己编写的算法,不使用Java中array.sort().这个使用的事优化后的快排
    ①冒泡排序:时间平均复杂度O(N^2)
    简述:两两比较,错误的交换。一轮比较,确定一个值

    //冒泡排序
            public int[] bubbleSort(int[] array){
                if(array.length==0|| array==null){
                    return null;
                }
                //控制排序趟数
                for (int i = 0; i <array.length ; i++) {
                    //控制两两排序交换
                    for (int j = 0; j <array.length-1-i; j++) {
                        if (array[j+1]<array[j]){
                        int tmp=array[j+1];
                        array[j+1]=array[j];
                        array[j]=tmp;
                        }
                    }
                }
               return array;
            }      
    

    ②选择排序: 时间平均复杂度O(N^2)
    简述:第一个值与每一个都比较,确定第一个,之后再第二个去比较...一直到n个结束

    //选择排序
         public int[] selectionSort(int[] array ){
             if(array.length==0|| array==null){
                 return null;
             }
             for (int i = 0; i <array.length; i++) {
                 //定义最小值的索引
                 int min=i;
                 for (int j = i; j <array.length ; j++) {
                     if(array[i]>array[j]){
                         min=j;
                     }
                 }
                 int temp=array[i];
                 array[i]=array[min];
                 array[min]=temp;
             }
             return array;
         }
    

    ③插入排序:时间平均复杂度O(N^2)
    简述:从第二个数开始,把这个数和前面的数比较,并放在合理位置

    插入排序
       //插入排序
        public int[] quickSort(int[] array ){
    
            if(array.length==0|| array==null){
                return null;
            }
            int  current;
            for (int i = 0; i <array.length-1; i++) {
                //临时存下array的值
            current =array[i+1];
    
            int preindex=i;
            //前面相当于都是有序的,后面前面的依次比较,找到合适的位置后就插入,不能插入的时候则,数组是索引往前移动
            while(preindex>=0&&current<array[preindex]){
                array[preindex+1]=array[preindex];
                preindex--;
            }
            array[preindex+1]=current;
            }
            return array;
        }
    

    ④希尔排序:时间平均复杂度O(nlogn)
    简述:它会优先比较距离较远的元素,希尔排序是把记录按下表的一定增量分组,对每组使用直接插入排序算法排序;随着增量逐渐减少,每组包含的关键词越来越多,当增量减至1时,整个文件恰被分成一组,算法便终止。

        //希尔排序
        public int[] xierSort(int[] array ) {
    
            if (array.length == 0 || array == null) {
                return null;
            }
            int len = array.length;
            int temp, gap = len / 2;
            //希尔增量因子大于0的时候.及继续循环,到gap/2不在大于零
            while (gap > 0) {
                //按照增量因子,把该列分成多组,i从增量因子开始
                for (int i = gap; i < len; i++) {
                    //用来存放中间值
                    temp = array[i];
                    //i 对应配对的排序中对应的值,对应的值可能存在多个,多值通过直接排序的方法
                    int prindex = i - gap;
    
                    while (prindex >= 0 && array[prindex] > temp) {
                     array[prindex+gap]=array[prindex];
                     prindex-=gap;
                    }
                    array[prindex+gap]=temp;
                }
                gap=gap/2;
            }
    return array;
        }
    }
    

    ⑤归并排序:时间平均复杂度O(nlogn)

         //归并排序
         public int[] MergeSort(int[] array ){
    
         if(array.length<2){return array;}
         int mid = array.length/2;
         int[] left  =Arrays.copyOfRange(array,0,mid);
         int[] right =Arrays.copyOfRange(array,mid,array.length);
         return merge(MergeSort(left),MergeSort(right));
        }
        //归并排序
       public int[] merge(int[] left,int[] right){
       int[] result = new int[left.length+right.length];
       for (int index=0,i=0,j=0;index<result.length;index++) {
           if (i >= left.length) {
               result[index] = right[j++];
           } else if (j >= right.length) {
               result[index] = left[i++];
           } else if (left[i] > right[j]) {
               result[index] = right[j++];
           } else {
               result[index] = left[i++];
           }
       }
      return result;
       }
    }
    

    ⑥快速排序:时间平均复杂度O(nlogn)

    ⑦堆排序:时间平均复杂度O(nlogn)
    ⑧桶排序:时间平均复杂度O(n+k)
    ⑨计数排序:时间平均复杂度O(n+k)
    ⑩基数排序:时间平均复杂度O(n*k)

    the cards are stored in the array

    1.Reverse thinking draw order, use queue to solve

    相关文章

      网友评论

          本文标题:2019-04-27.5-12 未完成

          本文链接:https://www.haomeiwen.com/subject/mblknqtx.html