zoukankan      html  css  js  c++  java
  • M面经prepare: Shuffle a deck

    设计一个shuffle card

    用了java. Random Class

     1 package Random;
     2 import java.util.*;
     3 
     4 public class Solution {
     5     static int cardNum = 10;
     6     public int[] shuffle(int[] cards) {
     7         //ArrayList<Integer> res = new ArrayList<Integer>();
     8         Random rnd = new Random();
     9         for(int i=0; i<cardNum; i++){
    10             int rand = rnd.nextInt(cardNum-i)+i;
    11             int temp = cards[i];
    12             cards[i] = cards[rand];
    13             cards[rand] = temp;
    14         }
    15         return cards;
    16     }
    17 
    18     /**
    19      * @param args
    20      */
    21     public static void main(String[] args) {
    22         // TODO Auto-generated method stub
    23         Solution sol = new Solution();
    24         int[] array = new int[cardNum];
    25         for (int i=0; i<cardNum; i++) array[i] = i;
    26         int[] res = sol.shuffle(array);
    27         System.out.println(Arrays.toString(res));
    28     }
    29 
    30 }
  • 相关阅读:
    LibreOJ2095
    Codeforces
    LibreOJ2241
    LibreOJ2044
    LibreOJ2043
    LibreOJ2045
    LibreOJ2042
    LibreOJ2097
    洛谷P4175
    POJ2888
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/5194193.html
Copyright © 2011-2022 走看看