解决我的面试问题 [英] Solve my interview questions

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

问题描述

为以下问题编写程序?

您将获得一个数字数组,其中包含0到n之间的所有数字,有些是随机的

订单,除了缺少一个数字并重复另一个数字(因此,数组中仍有n个数字)。找到缺失和重复的数字

Write a program for the following problem?
You are given an array of number that contain all numbers from 0 to n, in some random
Order, except that one number is missing and another number is repeated (thus, there are still n numbers in the array). Find the missing and repeated number

推荐答案

我在这里为您提供两种方法:



1.使用 BitArray [ ^ ]和设置与您正在迭代的数组的当前数对应的位,但在设置之前进行测试,因为如果已经设置,则您将拥有重复的条目。完成数组后,迭代BitArray以找到未设置的位。该位的索引是数组中缺少的数字。



2.按升序排序数组。迭代你的数组并计算当前元素减去前一个元素。这些情况存在:

- 差异是一个 - >一切都很好

- 差异为零 - >你找到了你的副本

- 差异是两个 - >你找到了缺少的元素(以前的元素+ 1或当前元素 - 1)



这真的很容易实现。



问候,

- Manfred
Here I offer you two approaches:

1. Use a BitArray[^] and set the bit corresponding to the current number of the array you are iterating over, but test before you set because if it already is set you'll have your duplicate entry. After you're done with your array iterate over the BitArray to find the bit that isn't set. The index of this bit is the number that was missing from the array.

2. Sort your array in ascending. Iterate over your array and calculate the current element minus the former element. These cases exist:
- The difference is one -> Everything is fine
- The difference is zero -> You've found your duplicate
- The difference is two -> You've found the missing element (former element + 1 or current element - 1)

That's really quite easy to implement.

Regards,
— Manfred


static void Main(string[] args)
       {
           // 2 repeated , and 3 missing in the following array
           int[] values = {1,2,2,4,5 };


           Array.Sort(values);
           int leastValue = values[0];
           int highestValue = values[values.Length-1];

           int[] missingValues = new int[highestValue-leastValue+1];
           int[] repeatedValues = new int[highestValue-leastValue+1];
           int[] distinctValues = new int[highestValue - leastValue + 1];
           int j = 0;
           int k = 0;
           int l = 0;

           //Find Missing Numbers
           for (int i = leastValue; i < highestValue; i++)
           {
               if (!values.Contains(i))
               {
                   missingValues[j]= i;
                   j++;
               }

           }


           //Find Repeated Values
           for (int i = 0; i < values.Length; i++)
           {
               if (!distinctValues.Contains(values[i]))
               {
                   distinctValues[k] = values[i];
                   k++;
               }
               else
               {
                   repeatedValues[l] = values[i];
                   l++;
               }
           }

           Console.WriteLine("************MISSING VALUES************************");
           foreach (var i in missingValues)
               Console.WriteLine(i);

           Console.WriteLine("************REPEATED  VALUES************************");
           foreach (var i in repeatedValues)
               Console.WriteLine(i);

           Console.ReadLine();





       }





试试吧上面的代码它会帮助你。

Prasad


这篇关于解决我的面试问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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