Python:每日一题 72

2017-11-27 14:22:00
六月
来源:
http://bbs.fishc.com/thread-93788-1-1.html
转贴 594
今天给大家带来一道算法题~




Given an unsorted array of integers, find the smallest number in the array, the largest number in the array, and the smallest number between the two array bounds that is not in the array.

For instance, given the array [-1, 4, 5, -23, 24], the smallest number is -23, the largest number is 24, and the smallest number between the array bounds is -22. You may assume the input is well-formed.

You solution should return an array [smallest, minimumAbsent, largest]

The smallest integer should be the integer from the array with the lowest value.

The largest integer should be the integer from the array with the highest value.

The minimumAbsent is the smallest number between the largest and the smallest number that is not in the array.

  1. minMinMax([-1, 4, 5, -23, 24]); //[-23, -22, 24]
  2. minMinMax([1, 3, -3, -2, 8, -1]); //[-3, 0, 8]
  3. minMinMax([2, -4, 8, -5, 9, 7]); //[-5, -3,9]

英文看不懂?
回复给你中文版!


给定一个未排序的整数数组,找到数组中最小的数字,数组中最大的数字以及不在数组中的两个数组界限之间的最小数字。

例如,给定数组[-1,4,5,23,24],最小数为-23,最大数为24,数组界限之间的最小数为-22。 您可以假设输入格式正确。

你的解决方案应该返回一个数组[最小,最小,最大]

最小的整数应该是数组中最小值的整数。

最大的整数应该是数组中最大值的整数。

minimumAbsent是不在数组中的最大和最小数之间的最小数。


为了方便理解,下面是例子:

  1. minMinMax([-1, 4, 5, -23, 24]); //[-23, -22, 24]
  2. minMinMax([1, 3, -3, -2, 8, -1]); //[-3, 0, 8]
  3. minMinMax([2, -4, 8, -5, 9, 7]); //[-5, -3,9]
复制代码





大家一起来啊

解决方案:


  1. def minMinMax(arr):
  2.     s, mi, ma = set(arr), min(arr), max(arr)
  3.     return [mi, next(x for x in range(mi+1, ma) if x not in s), ma]
复制代码
发表评论
评论通过审核后显示。