JZ50-数组中重复的数字

【题目描述】

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

请找出数组中第一个重复的数字:
例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是第一个重复的数字2。

返回描述:
如果数组中有重复的数字,函数返回true,否则返回false。
如果数组中有重复的数字,把重复的数字放到参数duplication[0]中。(ps:duplication已经初始化,可以直接赋值使用。)

【解法】
1.哈希法

//参照JZ40 哈希法,不可进行操作,for(const int k : numbers)只能在vectornumbers使用
在这里插入图片描述

class Solution {
public:
    // Parameters:
    //        numbers:     an array of integers
    //        length:      the length of array numbers
    //        duplication: (Output) the duplicated number in the array number
    // Return value:       true if the input is valid, and there are some duplications in the array number
    //                     otherwise false
    bool duplicate(int numbers[], int length, int* duplication) {
        vector<bool> f(length, false);
        for (int i=0; i<length; ++i) {
            if (!f[numbers[i]]) {
                f[numbers[i]] = true; 
            }
            else {
            //已经被统计过的数字变为true,进入else
                *duplication = numbers[i];
                return true;
            }
        }
        return false;
    }
};

在这里插入图片描述
可以这么做的原因是,数组中的数字范围在0~ n-1

2.in-place算法

此算法,适合于解找到重复数字中的最小数字,而不是第一个(具体见案例不通过示意图)

//着重抓住数据的范围是0-n-1这个点
在这里插入图片描述

class Solution {
public:
 // Parameters:
 //        numbers:     an array of integers
 //        length:      the length of array numbers
 //        duplication: (Output) the duplicated number in the array number
 // Return value:       true if the input is valid, and there are some duplications in the array number
 //                     otherwise false
 bool duplicate(int numbers[], int length, int* duplication) {
     for (int i=0; i<length; ++i) {
         // 不相等就一直交换
         while (i != numbers[i]) {
             if (numbers[i] != numbers[numbers[i]]) {
                 swap(numbers[i], numbers[numbers[i]]);
             }
             else {
                 *duplication = numbers[i];
                 return true;
             }
         }

     }
     return false;
 }
};

在这里插入图片描述
程序执行过程分析:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
汇编语言是一种低级语言,用于编写计算机程序的指令集。在汇编中,我们可以通过一系列的指令来实现数组的操作。 要将grade数组中的成绩放到rank数组中,可以使用以下步骤: 1. 首先,我们需要定义并初始化两个数组grade和rank。在汇编语言中,可以使用.data段来定义数组,并通过指令为数组元素赋初值。 2. 接下来,我们可以使用循环来遍历grade数组。可以使用比较指令来比较当前成绩和之前的成绩,以确定当前成绩在rank数组中的位置。 3. 对于每个成绩,我们需要根据比较结果将其插入到rank数组的正确位置。这可以通过使用移动指令来实现。如果当前成绩比之前的成绩低,则将之前的成绩后移一个位置,然后将当前成绩插入到空出的位置。 4. 最后,重复上述步骤,直到遍历完grade数组中的所有成绩。 以下是一个简单的汇编代码示例,用于演示如何将grade数组中的成绩放到rank数组中: ``` .data grade DW 90, 80, 95, 85, 70 rank DW 0, 0, 0, 0, 0 .code mov cx, 5 ; 设置循环次数为数组的长度 mov si, 0 ; 初始化grade数组的索引 loop_start: mov ax, [grade+si*2] ; 获取当前成绩 ; 比较当前成绩与之前成绩,并找到插入位置 mov di, si cmp di, 0 ; 检查是否是第一个成绩 jz insert cmp ax, [rank+di*2-2] ; 比较当前成绩和之前的成绩 jl shift ; 当前成绩较低,应将之前的成绩后移一个位置 insert: mov [rank+di*2], ax ; 将当前成绩插入到rank数组中 inc si ; 移到grade数组的下一个元素 loop loop_start ; 重复循环 shift: mov bx, di mov dx, [rank+bx*2-2] ; 保存要后移的成绩 mov [rank+di*2], dx ; 后移成绩 cmp di, 0 ; 检查是否是第一个成绩 jz insert dec di ; 移到rank数组的前一个位置 jmp shift ; 继续比较 end: ``` 通过以上的汇编代码,我们可以将grade数组中的成绩按照从高到低的顺序放入rank数组中。答案长度超过了300字,但已经包含了大致的思路和一个简单示例的汇编代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值