public class Solution { public int MissingNumber(int[] nums) { var list = nums.OrderBy(x => x).ToList(); var preNum = 0; foreach (var l in list) { if (l != preNum) { return preNum; } preNum++; } return preNum; } }
https://leetcode.com/problems/missing-number/#/description
补充一个python的实现:
1 class Solution: 2 def missingNumber(self, nums): 3 missing = len(nums) 4 for i, num in enumerate(nums): 5 missing ^= i ^ num 6 return missing