I was working on a leetcode question [1] and noticed something that caught my eye.
When I write a line of code as:
nums[i] = nums[(i++)+count];
I pass all the tests and my answer is accepted. If however I change the same line to:
nums[i++] = nums[i+count];
I get an ArrayOutOfBounds exception. Why is this?
Here is the full code:
public void moveZeroes(int[] nums) {
int count = 0, i = 0;
while(i+count < nums.length){
if(nums[i+count] == 0)
count++;
else
nums[i] = nums[(i++)+count];
}
for(;i<nums.length; i++)
nums[i] = 0;
}