帮助需要写一个算法算法

由网友(喝点快乐)分享简介:下面是对问题的描述给定一个正整数N,写的函数返回的大小为N的整数的数组,含有以随机的顺序从1到N的数字。每个号码从1到N必须出现一次,不能重复。Given an integer N, write a function which returns an integer array of size N, contain...

下面是对问题的描述

给定一个正整数N,写的函数返回的大小为N的整数的数组,含有以随机的顺序从1到N的数字。每个号码从1到N必须出现一次,不能重复。

Given an integer N, write a function which returns an integer array of size N, containing the numbers from 1 to N in a random order. Each number from 1 to N must appear once and must not repeat.

什么是你的算法的运行时间? 可你的算法加以改进?

例如:如果你给出的数字4,你的输出必须产生像4213,2413,3124,等等

For example: if you are given the number 4, your output must generate something like 4213, 2413, 3124, etc.

无效的产出将是1123,4444,244。

Invalid outputs would be 1123, 4444, 244.

任何想法来解决这个问题?

Any ideas to solve the problem?

推荐答案

下面是一个暗示:查什么是费雪耶茨洗牌

Here is a hint: look up what a Fisher-Yates shuffle is.

阅读全文

相关推荐

最新文章