Minimalist C code, no additional space - code golf? Come play!


  • 1
    O

    Really didn't think this counted as medium difficulty, so instead decided to play code golf. Any takers?

    int missingNumber(int* nums, int numsSize) {
        while(--numsSize)
            nums[numsSize-1]^=nums[numsSize]^(numsSize+1);
        return *nums^1;
    }

  • 0
    O
    This post is deleted!

  • 0
    O

    Getting better with pointers?

    int missingNumber(int* nums, int numsSize) {
        while(numsSize>1)
            *nums^=*nums++^numsSize--;
        return *nums^1;
    }

  • 0
    This post is deleted!

  • 0
    O

    Character reduction cheating:

    int missingNumber(int* a, int b) {
      while(b>1)
          *a^=*a++^b--;
      return *a^1;
    }

  • 0

    You still have way too many space characters :-)


  • 0

    I tried beating you but could only get the same length. But I think this variation is safer, as it doesn't rely on an undefined subexpression evaluation order:

    int missingNumber(int*a,int n){while(--n)*a^=n+1^a[n];return*a^1;}

  • 0
    O

    I had been concerned about order of expressions- my attempts got riskier and riskier. And I guess as your code shows didn't get much out of it (I think I had your code at one point, but before I realized changing variable names was important so I was trying to avoid another use of numSize(/n) variable).


Log in to reply
 

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