268. Missing Number
Task text is translated from Russian for the selected interface language. Code is left unchanged.
Дан array nums, содержащий n различных чисел в диапазоне [0, n]. return единственное number в этом диапазоне, которого нет в arrayе.
Example:
Input: nums = [3,0,1]
Output: 2
Explanation: n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums.
C# solution
matched/originalpublic class Solution {
public int MissingNumber(int[] nums) {
Array.Sort(nums);
if (nums[nums.Length - 1] != nums.Length) {
return nums.Length;
} else if (nums[0] != 0) {
return 0;
}
for (int i = 1; i < nums.Length; i++) {
int expectedNum = nums[i - 1] + 1;
if (nums[i] != expectedNum) {
return expectedNum;
}
}
return -1;
}
}
C++ solution
auto-draft, review before submit#include <bits/stdc++.h>
using namespace std;
// Auto-generated C++ draft from the C# solution. Review containers, LINQ and helper types before submit.
class Solution {
public:
public int MissingNumber(vector<int>& nums) {
sort(nums.begin(), nums.end());
if (nums[nums.size() - 1] != nums.size()) {
return nums.size();
} else if (nums[0] != 0) {
return 0;
}
for (int i = 1; i < nums.size(); i++) {
int expectedNum = nums[i - 1] + 1;
if (nums[i] != expectedNum) {
return expectedNum;
}
}
return -1;
}
}
Java solution
matched/originalclass Solution {
public int missingNumber(int[] nums) {
Arrays.sort(nums);
if (nums[nums.length-1] != nums.length) {
return nums.length;
} else if (nums[0] != 0) {
return 0;
}
for (int i = 1; i < nums.length; i++) {
int expectedNum = nums[i-1] + 1;
if (nums[i] != expectedNum) {
return expectedNum;
}
}
return -1;
}
}
JavaScript solution
matched/originalvar missingNumber = function(nums) {
nums.sort((a, b) => a - b)
if (nums[nums.length - 1] != nums.length) {
return nums.length
} else if (nums[0] != 0) {
return 0
}
for (let i = 1; i < nums.length; i++) {
let expectedNum = nums[i - 1] + 1
if (nums[i] != expectedNum) {
return expectedNum
}
}
return -1
}
Python solution
matched/originalclass Solution:
def missingNumber(self, nums):
nums.sort()
if nums[-1] != len(nums):
return len(nums)
elif nums[0] != 0:
return 0
for i in range(1, len(nums)):
expected_num = nums[i - 1] + 1
if nums[i] != expected_num:
return expected_num
return -1
Go solution
matched/originalfunc missingNumber(nums []int) int {
sort.Ints(nums)
if nums[len(nums)-1] != len(nums) {
return len(nums)
} else if nums[0] != 0 {
return 0
}
for i := 1; i < len(nums); i++ {
expectedNum := nums[i-1] + 1
if nums[i] != expectedNum {
return expectedNum
}
}
return -1
}
Algorithm
1️⃣
Сначала отсортируйте array nums.
2️⃣
Проверьте особые случаи: убедитесь, что number 0 находится в начале arrayа, а number n — в конце.
3️⃣
Пройдитесь по отсортированному arrayу и для каждого индекса проверьте, что number на этом индексе соответствует ожидаемому (предыдущее number плюс один). Как только вы обнаружите несоответствие, return ожидаемое number.
😎
Vacancies for this task
Active vacancies with overlapping task tags are shown.
There are no active vacancies yet.