Created
August 6, 2023 08:38
-
-
Save ritik-agrawal/48fc4fab73e602da680ae22aa5b52628 to your computer and use it in GitHub Desktop.
Leet Code: Move the zeros to the last of the given int[] while maintaining the relative order of the non zero numbers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public void moveZeroes(int[] nums) { | |
var len = nums.length; | |
var mark = 0; | |
for (int i = 0; i < len; i++){ | |
if (nums[i] != 0){ | |
nums[mark] = nums[i]; | |
mark++; | |
} | |
} | |
for (int j = mark; j < len; j++){ | |
nums[j] = 0; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Observation
The above problem was solved using two approaches by me and the most optimal solution was found online.
Approach 1
This approach beats 12% of the total solutions submitted with a runtime of 30 ms.
Approach 2
The above approach beats 42% of the total solutions submitted with a runtime of 2 ms.
Approach 3
The above approach beats 100% of the cases with a runtime of 1 ms.
A link to all the submissions. Click here