首页 > 编程语言 > 详细

数组排序Array.Sort(nums)

时间:2020-06-07 11:29:13      阅读:44      评论:0      收藏:0      [点我收藏+]

找出数组中重复的数字。


在一个长度为 n 的数组 nums 里的所有数字都在 0~n-1 的范围内。数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次。请找出数组中任意一个重复的数字。

示例 1:

输入:
[2, 3, 1, 0, 2, 5, 3]
输出:2 或 3
 

限制:

2 <= n <= 100000

 

代码:

public class Solution {
    public int FindRepeatNumber(int[] nums) {
        Array.Sort(nums);
        for(int i=0;i<nums.Length;i++){
            if(nums[i]==nums[i+1]){
                return nums[i];
            }
        }
        return 0;
    }
}
 
备注: 不占内存,时间效率低。

数组排序Array.Sort(nums)

原文:https://www.cnblogs.com/mcxdeboke/p/13059263.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!