如果对任意 $0 \le i < n$,都有 $\textit{nums}[i]=i$,则缺失的数字是 $n$。
[sol1-Java]
1 2 3 4 5 6 7 8 9 10 11 12
classSolution { publicintmissingNumber(int[] nums) { Arrays.sort(nums); intn= nums.length; for (inti=0; i < n; i++) { if (nums[i] != i) { return i; } } return n; } }
[sol1-C#]
1 2 3 4 5 6 7 8 9 10 11 12
publicclassSolution { publicintMissingNumber(int[] nums) { Array.Sort(nums); int n = nums.Length; for (int i = 0; i < n; i++) { if (nums[i] != i) { return i; } } return n; } }
[sol1-C++]
1 2 3 4 5 6 7 8 9 10 11 12 13
classSolution { public: intmissingNumber(vector<int>& nums){ sort(nums.begin(),nums.end()); int n = nums.size(); for (int i = 0; i < n; i++) { if (nums[i] != i) { return i; } } return n; } };
[sol1-JavaScript]
1 2 3 4 5 6 7 8 9 10
var missingNumber = function(nums) { nums.sort((a, b) => a - b); const n = nums.length; for (let i = 0; i < n; i++) { if (nums[i] !== i) { return i; } } return n; };
[sol1-TypeScript]
1 2 3 4 5 6 7 8 9 10
functionmissingNumber(nums: number[]): number { nums.sort((a, b) => a - b); constn: number = nums.length; for (let i = 0; i < n; i++) { if (nums[i] !== i) { return i; } } return n; };
[sol1-Golang]
1 2 3 4 5 6 7 8 9
funcmissingNumber(nums []int)int { sort.Ints(nums) for i, num := range nums { if num != i { return i } } returnlen(nums) }
[sol1-Python3]
1 2 3 4 5 6 7
classSolution: defmissingNumber(self, nums: List[int]) -> int: nums.sort() for i, num inenumerate(nums): if num != i: return i returnlen(nums)
classSolution { publicintmissingNumber(int[] nums) { Set<Integer> set = newHashSet<Integer>(); intn= nums.length; for (inti=0; i < n; i++) { set.add(nums[i]); } intmissing= -1; for (inti=0; i <= n; i++) { if (!set.contains(i)) { missing = i; break; } } return missing; } }
[sol2-C#]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
publicclassSolution { publicintMissingNumber(int[] nums) { ISet<int> set = new HashSet<int>(); int n = nums.Length; for (int i = 0; i < n; i++) { set.Add(nums[i]); } int missing = -1; for (int i = 0; i <= n; i++) { if (!set.Contains(i)) { missing = i; break; } } return missing; } }
[sol2-C++]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
classSolution { public: intmissingNumber(vector<int>& nums){ unordered_set<int> set; int n = nums.size(); for (int i = 0; i < n; i++) { set.insert(nums[i]); } int missing = -1; for (int i = 0; i <= n; i++) { if (!set.count(i)) { missing = i; break; } } return missing; } };
[sol2-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
var missingNumber = function(nums) { const set = newSet(); const n = nums.length; for (let i = 0; i < n; i++) { set.add(nums[i]); } let missing = -1; for (let i = 0; i <= n; i++) { if (!set.has(i)) { missing = i; break; } } return missing; };
[sol2-TypeScript]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
functionmissingNumber(nums: number[]): number { const set = newSet(); constn: number = nums.length; for (let i = 0; i < n; i++) { set.add(nums[i]); } letmissing: number = -1; for (let i = 0; i <= n; i++) { if (!set.has(i)) { missing = i; break; } } return missing; };
[sol2-Golang]
1 2 3 4 5 6 7 8 9 10 11
funcmissingNumber(nums []int)int { has := map[int]bool{} for _, v := range nums { has[v] = true } for i := 0; ; i++ { if !has[i] { return i } } }
[sol2-Python3]
1 2 3 4 5 6
classSolution: defmissingNumber(self, nums: List[int]) -> int: s = set(nums) for i inrange(len(nums) + 1): if i notin s: return i
classSolution { publicintmissingNumber(int[] nums) { intxor=0; intn= nums.length; for (inti=0; i < n; i++) { xor ^= nums[i]; } for (inti=0; i <= n; i++) { xor ^= i; } return xor; } }
[sol3-C#]
1 2 3 4 5 6 7 8 9 10 11 12 13
publicclassSolution { publicintMissingNumber(int[] nums) { int xor = 0; int n = nums.Length; for (int i = 0; i < n; i++) { xor ^= nums[i]; } for (int i = 0; i <= n; i++) { xor ^= i; } return xor; } }
[sol3-C++]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
classSolution { public: intmissingNumber(vector<int>& nums){ int res = 0; int n = nums.size(); for (int i = 0; i < n; i++) { res ^= nums[i]; } for (int i = 0; i <= n; i++) { res ^= i; } return res; } };
[sol3-JavaScript]
1 2 3 4 5 6 7 8 9 10 11
var missingNumber = function(nums) { let xor = 0; const n = nums.length; for (let i = 0; i < nums.length; i++) { xor ^= nums[i]; } for (let i = 0; i <= n; i++) { xor ^= i; } return xor; };
[sol3-TypeScript]
1 2 3 4 5 6 7 8 9 10 11
var missingNumber = function(nums) { letxor: number = 0; constn: number = nums.length; for (let i = 0; i < nums.length; i++) { xor ^= nums[i]; } for (let i = 0; i <= n; i++) { xor ^= i; } return xor; };
[sol3-Golang]
1 2 3 4 5 6
funcmissingNumber(nums []int) (xor int) { for i, num := range nums { xor ^= i ^ num } return xor ^ len(nums) }
[sol3-Python3]
1 2 3 4 5 6
classSolution: defmissingNumber(self, nums: List[int]) -> int: xor = 0 for i, num inenumerate(nums): xor ^= i ^ num return xor ^ len(nums)
classSolution { publicintmissingNumber(int[] nums) { intn= nums.length; inttotal= n * (n + 1) / 2; intarrSum=0; for (inti=0; i < n; i++) { arrSum += nums[i]; } return total - arrSum; } }
[sol4-C#]
1 2 3 4 5 6 7 8 9 10 11
publicclassSolution { publicintMissingNumber(int[] nums) { int n = nums.Length; int total = n * (n + 1) / 2; int arrSum = 0; for (int i = 0; i < n; i++) { arrSum += nums[i]; } return total - arrSum; } }
[sol4-C++]
1 2 3 4 5 6 7 8 9 10 11 12
classSolution { public: intmissingNumber(vector<int>& nums){ int n = nums.size(); int total = n * (n + 1) / 2; int arrSum = 0; for (int i = 0; i < n; i++) { arrSum += nums[i]; } return total - arrSum; } };
[sol4-JavaScript]
1 2 3 4 5 6 7 8 9
var missingNumber = function(nums) { const n = nums.length; let total = Math.floor(n * (n + 1) / 2); let arrSum = 0; for (let i = 0; i < n; i++) { arrSum += nums[i]; } return total - arrSum; };
[sol4-TypeScript]
1 2 3 4 5 6 7 8 9
var missingNumber = function(nums) { constn: number = nums.length; lettotal: number = Math.floor(n * (n + 1) / 2); letarrSum: number = 0; for (let i = 0; i < n; i++) { arrSum += nums[i]; } return total - arrSum; };
[sol4-Golang]
1 2 3 4 5 6 7 8 9
funcmissingNumber(nums []int)int { n := len(nums) total := n * (n + 1) / 2 arrSum := 0 for _, num := range nums { arrSum += num } return total - arrSum }
[sol4-Python3]
1 2 3 4 5 6
classSolution: defmissingNumber(self, nums: List[int]) -> int: n = len(nums) total = n * (n + 1) // 2 arrSum = sum(nums) return total - arrSum