C# solution: XOR & bucket sort


  • 0
    B

    Solution 1: XOR

    public class Solution 
    {
        public int MissingNumber(int[] nums) 
        {
            var result = nums.Length;
    
            for(int i = 0; i < nums.Length; i++)
            {
                result ^= i;
                result ^= nums[i];
            }
    
            return result;
        }
    }
    

    Solution 2: bucket sort

    public class Solution 
    {
        public int MissingNumber(int[] nums) 
        {
            for (int i = 0; i < nums.Length; i++)
            {
                while(nums[i] < nums.Length && nums[i] != i)
                {
                    var temp = nums[i];
                    nums[i] = nums[temp];
                    nums[temp] = temp;
                }
            }
    
            for (int i = 0; i < nums.Length; i++)
            {
                if (nums[i] != i)
                {
                    return i;
                }
            }
            
            return nums.Length;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.