Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] nums = {1,2,3}; Solution solution = new Solution(nums); // Shuffle the array [1,2,3] and return its result. Any permutation of [1,2,3] must equally lik…
PS:这是一道出境率极高的题目,记得去年参加校园招聘时我看到了3次,但是每次写的都不完善. 一.题目:链表的倒数第k个节点 题目:输入一个链表,输出该链表中倒数第k个结点.为了符合大多数人的习惯,本题从1开始计数,即链表的尾结点是倒数第1个结点.例如一个链表有6个结点,从头结点开始它们的值依次是1.2.3.4.5.6.这个链表的倒数第3个结点是值为4的结点. 链表的节点定义如下,这里使用的是C#来定义: public class Node { public int Data { get; set…
面试题12:打印1到最大的n位数 剑指offer题目12,题目如下 输入数字n,按顺序打印出1到最大的n位十进制数,比如输入3,则打印出1,2,3一直到最大的三位数999 方法一 和面试题11<数值的整数次方>类似,乍看觉得都是简单题目,找出最大的n位数,开始逐个打印,写出代码 public static void method_1(int n ) { int num = 1; int i = 1; while(i <= n) { num *= 10 i++; } for(i = 1;i…
18.2 Write a method to shuffle a deck of cards. It must be a perfect shuffle—in other words, each of the 52! permutations of the deck has to be equally likely. Assume that you are given a random number generator which is perfect. 这道题让我们实现一个洗牌的算法,实际上洗…
function shuffle(arr){ var len = arr.length; for(var i = 0;i<len -1;i++) { var idx = Math.floor(Math.random() * (len - 1)); console.log("idx",idx); var temp = arr[idx]; console.log("temp",temp); arr[idx] = arr[len - i - 1]; console.…
CARDS Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 1448 Accepted: 773 Description Alice and Bob have a set of N cards labelled with numbers 1 ... N (so that no two cards have the same label) and a shuffle machine. We assume that N i…