M面经prepare: Shuffle a deck
设计一个shuffle card
用了java. Random Class
package Random;
import java.util.*; public class Solution {
static int cardNum = 10;
public int[] shuffle(int[] cards) {
//ArrayList<Integer> res = new ArrayList<Integer>();
Random rnd = new Random();
for(int i=0; i<cardNum; i++){
int rand = rnd.nextInt(cardNum-i)+i;
int temp = cards[i];
cards[i] = cards[rand];
cards[rand] = temp;
}
return cards;
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Solution sol = new Solution();
int[] array = new int[cardNum];
for (int i=0; i<cardNum; i++) array[i] = i;
int[] res = sol.shuffle(array);
System.out.println(Arrays.toString(res));
} }
M面经prepare: Shuffle a deck的更多相关文章
- [CareerCup] 18.2 Shuffle Cards 洗牌
18.2 Write a method to shuffle a deck of cards. It must be a perfect shuffle—in other words, each of ...
- POJ - 1978 Hanafuda Shuffle
最初给牌编号时,编号的顺序是从下到上:洗牌时,认牌的顺序是从上到下.注意使用循环是尽量统一“i”的初始化值,都为“0”或者都为“1”,限界条件统一使用“<”或者“<=”. POJ - 19 ...
- HDU 4759 Poker Shuffle
Poker Shuffle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- codeforces Gym 100187J J. Deck Shuffling dfs
J. Deck Shuffling Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/pro ...
- HDU 4759 Poker Shuffle(2013长春网络赛1001题)
Poker Shuffle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- Mini projects #6 ---- Blackjack
课程全名:An Introduction to Interactive Programming in Python,来自 Rice University 授课教授:Joe Warren, Scott ...
- hdu 4759 大数+找规律 ***
题目意思很简单. 就是洗牌,抽出奇数和偶数,要么奇数放前面,要么偶数放前面. 总共2^N张牌. 需要问的是,给了A X B Y 问经过若干洗牌后,第A个位置是X,第B个位置是Y 是不是可能的. Ja ...
- Java基础之集合框架——使用堆栈Stack<>对象模拟发牌(TryDeal)
控制台程序. public enum Rank { TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, A ...
- Hard 随机洗牌函数 @CareerCup
第i个元素和index在[i,length-1]之间的一个数随机交换 package Hard; import CtCILibrary.AssortedMethods; /** * * Write a ...
随机推荐
- mysql从只有一个备份文件(多个数据库的备份)中恢复数据到指定数据库
mysql -uroot -p 要恢复的数据库的名字 --one-database<备份文件
- PHP5.4安装xhprof扩展[不要去pecl下载]
HP5.3或之前版本可以去pecl(http://pecl.php.net)下载xhprof扩展安装. 但pecl上的版本不支持PHP5.4 可以到github上的xhprof库中下载:https:/ ...
- laravle faker
1.编辑 /database/factories/ModelFactory,添加新的类模型填充 $factory->define(App\Post::class, function (Faker ...
- Apache Kafka源码分析 - PartitionStateMachine
startup 在onControllerFailover中被调用, initializePartitionState private def initializePartitionState() { ...
- 蓝牙Host Controller Interface笔记
1.概述 HCI提供了一个统一的使用蓝牙控制器(BR/EDR Controller,BR/EDR/LE Controller,LE Controller,AMP Controller等)的方法 ...
- 【转】Servlet与web.xml的配置
Web.xml常用元素<web-app><display-name></display-name>定义了WEB应用的名字<description>< ...
- Socket通信原理探讨(C++为例) good
http://www.cnblogs.com/xufeiyang/articles/4878096.html http://www.cnblogs.com/xufeiyang/articles/453 ...
- Android HttpHeader的坑
昨天遇到一个问题,代码如下: private String getSessionId(HttpResponse response) { // TODO Auto-generated method st ...
- JavasScript判断输入框不为空
<form name="form1" method="POST" action="add.php"> <table wid ...
- Java学习-014-文本文件写入实例源代码(两种写入方式)
此文源码主要为应用 Java 读取文本文件内容实例的源代码.若有不足之处,敬请大神指正,不胜感激! 第一种:文本文件写入,若文件存在则删除原文件,并重新创建文件.源代码如下所示: /** * @fun ...