帮助需要写一个算法 [英] Help needed for writing an algorithm

查看:92
本文介绍了帮助需要写一个算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是对问题的描述

给定一个正整数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.

这篇关于帮助需要写一个算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆