問題
Walk through the list once and track both the smallest and the largest value.
Implement min_and_max(numbers).
Input
numbers: A non-empty list of integers.
Output
Return [smallest, largest].
制約
1 ≤ length ≤ 100,000
例
例 1
入力:
numbers = [4, -2, 9, 0]
期待される出力:
expected = [-2, 9]
入力:
std::vector<long long> numbers = {4, -2, 9, 0};
期待される出力:
std::vector<long long> expected = {-2, 9};
入力:
long[] numbers = new long[] {4, -2, 9, 0};
期待される出力:
long[] expected = new long[] {-2, 9};
解説
The smallest value is -2 and the largest is 9: [-2, 9].
例 2
入力:
numbers = [7]
期待される出力:
expected = [7, 7]
入力:
std::vector<long long> numbers = {7};
期待される出力:
std::vector<long long> expected = {7, 7};
入力:
long[] numbers = new long[] {7};
期待される出力:
long[] expected = new long[] {7, 7};
解説
With one value it is both the smallest and largest: [7, 7].
参考資料
この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。