找出数组中重复的数字(c语言)

让人瑟瑟发抖的面试题


来我们看一下题目
在一个 长度为n的数组里的所有数字都在0~n-的范围内。数组中某些数字是重复的,但不知道有几个数字重复伦理,也不知道每个数字重复了多少次,找出任意一个重复的数字
注意:时间复杂度O(n),空间复杂度O(1)

怎么解决勒???
分析:利用题目中0~n-1范围,可以运用数组下标和数组内容进行比较
if (arr[i] != arr[arr[i]]),如果不相等时,进行调换,相等时,直接返回值
来看看代码

#include<stdio.h>
#define SIZE(arr) sizeof(arr)/sizeof(arr[0])//数组长度

void Swap(int *left, int *right)
{
    int tmp = *left;
    *left = *right;
    *right = tmp;
}
int duplicate(int arr[],int len)
{
    int i;
    if (len < 0)
    {
        return 0;
    }
    for (i = 0; i < len; i++)
    {
        if (arr[i] < 0 || arr[i]>len - 1)//限定数字大小
        {
            return 0;
        }
        while (arr[i] != i)
        {
            if (arr[i] != i)
            {
                if (arr[i] != arr[arr[i]])//数组中数字是否等于以数字为下标的数字
                {
                    Swap(&arr[i], &arr[arr[i]]);
                }
                else
                {
                    return arr[i];
                }
            }
        }
    }
    return 0;
}
int main()
{
    int arr[] = {2,3,1,0,2,5,3};
    printf("%d", duplicate(arr, SIZE(arr)));
    return 0;
}

总结:数组中数据给定范围之后,可以多利用下标 i 进行求解

原文地址:https://blog.51cto.com/14233078/2436533

时间: 2024-07-30 04:42:33

找出数组中重复的数字(c语言)的相关文章

442. 找出数组中重复的元素 Find All Duplicates in an ArrayGiven an array of integers

Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements that appear twice in this array. Could you do it without extra space and in O(n) runtime? Example: Input: [4,3,2,7,

1-找出数组中重复的数字

力扣-面试题03. 数组中重复的数字 修改数组 不修改数组 题目描述: 在一个长度为 n 的数组 nums 里的所有数字都在 0-n-1 的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次.请找出数组中任意一个重复的数字. 输入: [2,3,1,0,2,5,3] 输出: 2or3 //把每个数放到对应的位置=>i=nums[i] class Solution { public: int duplicateInArray(vector<int>&

经典算法学习——快速找出数组中两个数字,相加等于某特定值

这个算法题的描述如下:快速找出一个数组中的两个数字,让这两个数字之和等于一个给定的值.目前我假设数组中的都是各不相等的整数.这道题是我在一次面试中被问到的,由于各种原因,我没回答上来,十分尴尬.其实这道题十分简单,我们使用相对巧妙的方法来实现下.注意不使用两层循环的元素遍历.示例代码上传至:https://github.com/chenyufeng1991/SumTo100 . 算法描述如下: (0)首先对原数组进行排序,成为递增数组: (1)对排序后的数组头部i [0]和数组尾部j [n-1]

C# 找出数组中重复次数最多的数值

给定一个int数组,里面存在重复的数值,如何找到重复次数最多的数值呢? 这是在某社区上有人提出的问题,我想到的解决方法是分组. 1.先对数组中的所有元素进行分组,那么,重复的数值肯定会被放到一组中: 2.将分组进行排序,排序条件是分组中的元素个数: 3.元素数量最多的那个分组中的数值就是重复次数最多的. 基于以上思路,可以写出以下代码: // 示例数组,90重复4次,1重复2次,3重复3次 int[] arr = { 1, 1, 3, 3, 3, 7, 50, 15, 15, 90, 90, 9

Java实现找出数组中重复次数最多的元素以及个数

/**数组中元素重复最多的数 * @param array * @author shaobn * @param array */ public static void getMethod_4(int[] array){ Map<Integer, Integer> map = new HashMap<>(); int count = 0; int count_2 = 0; int temp = 0; for(int i=0;i<array.length;i=i+count){

JavaScript 找出数组中重复的元素

实现检测数组重复元素的功能,需要注意一点的是,多个(2个或2个以上)重复元素,我们只需要挑出一个来就可以了. <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>文字循环显示</title> <script type="text/javascript"> var sear=function(arr) { var

找出数组中特定和数字下标(JAVA)

比如: 输入: numbers={2, 7, 11, 15}, target=9 输出: index1=1, index2=2 1 public class _003TwoSum { 2 3 public static void main(String[] args) { 4 int a[]={3,22,4,7,8,22}; 5 6 display(twoSum3(a,30)); 7 } 8 //暴力搜索法 9 public static int[] twoSum(int[] numbers,

查找数组中重复的数字

题目来源于<剑指Offer>中的面试题3:找出数组中重复的数字. // 题目:在一个长度为n的数组里的所有数字都在0到n-1的范围内.数组中某些数字是重复的,但不知道有几个数字重复了, // 也不知道每个数字重复了几次.请找出数组中任意一个重复的数字.例如,如果输入长度为7的数组{2, 3, 1, 0, 2, 5, 3}, // 那么对应的输出是重复的数字2或者3. 解决方法有多种,包括数组排序,哈希表法,以及作者推荐的重排数组法.此处介绍自己的一个做法,以空间换时间,通过新建数组来实现快速查

【剑指offer】3、数组中重复的数字

题目一:找出数组中重复的数字. ......S2 ..... 1 class Solution { 2 public: 3 // Parameters: 4 // numbers: an array of integers 5 // length: the length of array numbers 6 // duplication: (Output) the duplicated number in the array number 7 // Return value: true if t