描述:
写一个方法consecutive(arr),参数为一个整数数组,返回这个数组中最小值到最大值之间所需连续数字的个数(不包含数组中已有的数字)
例如:
如果数组为 [4, 8, 6] 那么应该输出2。 因为4到8之间还需要两个数字(5,7)才能把这个数组变成连续数字。参数数组中每个数字都是唯一的。
MyCode:
using System.Linq;
public class Kata
{
public static int Consecutive(int[] arr)
{
if(arr.Length < 2)
return 0;
int x = arr.Min();
int count = 0;
while(x <= arr.Max())
{
x++;
count++;
}
return (count - arr.Length);
}
}
CodeWar:
using System.Linq;
public class Kata
{
public static int Consecutive(int[] arr)
{
return arr.Length==0 ? 0 : arr.Max()-arr.Min()-arr.Length+1;
}
}