码迷,mamicode.com
首页 > 编程语言 > 详细

【面试题】向一个长度为100的int数组,插入1-100的随机数,不能重复

时间:2016-02-20 19:07:27      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:

 1  public int[] GetNoRepeatArrayy(int arrLength)
 2         {
 3             int[] array = new int[arrLength];
 4             IList<int> list = new List<int>();
 5             //准备不重复的数据
 6             for (int i = 0; i < array.Length; i++)
 7             {
 8                 list.Add(i);
 9             }
10             //将不重复的数据随机插入到数组中
11             for (int j = (list.Count - 1); j > -1; j--)
12             {
13                 //获得数据的随机索引
14                 int index = new Random(Guid.NewGuid().GetHashCode()).Next(0, list.Count);
15                 //给数组赋值
16                 array[j] = list[index];
17                 //删除废弃数据,避免重复数据插入到数组中
18                 list.RemoveAt(index);
19             }
20             return array;
21         } 

 

【面试题】向一个长度为100的int数组,插入1-100的随机数,不能重复

标签:

原文地址:http://www.cnblogs.com/5tomorrow/p/5203713.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!