Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public int removeDuplicates(int[] nums) {
- // Numbers nums[0] and nums[1] will stay where they are.
- int insertPosition = 2;
- // We will iterate over numbers starting from i = 2,
- // and numbers that we need at insertPosition one-by-one.
- for (int i = 2; i < nums.length; i++) {
- if (nums[i] != nums[insertPosition - 2]) {
- nums[insertPosition++] = nums[i];
- }
- }
- return insertPosition;
- // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
- // nums = [1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3].
- // insertPosition = 2 ^
- // i = 2 ^
- // nums[i] == nums[insertPosition - 2], so skip this 1 as it was included twice.
- // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
- // nums = [1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3].
- // insertPosition = 2 ^
- // i = 3 ^
- // nums[i] == nums[insertPosition - 2], so skip this 1 as it was included twice.
- // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
- // nums = [1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3].
- // insertPosition = 2 ^
- // i = 4 ^
- // nums[i] == nums[insertPosition - 2], so skip this 1 as it was included twice.
- // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
- // nums = [1, 1, *2*, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3].
- // insertPosition = 2 ^
- // i = 5 ^
- // nums[i] != nums[insertPosition - 2], so put 2 at insertPosition = 2
- // and increment insertPosition by 1.
- // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
- // nums = [1, 1, 2, *2*, 1, 2, 2, 2, 2, 2, 3, 3, 3].
- // insertPosition = 3 ^
- // i = 6 ^
- // nums[i] != nums[insertPosition - 2], so put 2 at insertPosition = 3
- // and increment insertPosition by 1.
- // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
- // nums = [1, 1, 2, 2, 1, 2, 2, 2, 2, 2, 3, 3, 3].
- // insertPosition = 4 ^
- // i = 7 ^
- // nums[i] != nums[insertPosition - 2], so skip this 2 as it was included twice.
- // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
- // nums = [1, 1, 2, 2, 1, 2, 2, 2, 2, 2, 3, 3, 3].
- // insertPosition = 4 ^
- // i = 8 ^
- // nums[i] != nums[insertPosition - 2], so skip this 2 as it was included twice.
- // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
- // nums = [1, 1, 2, 2, 1, 2, 2, 2, 2, 2, 3, 3, 3].
- // insertPosition = 4 ^
- // i = 8 ^
- // nums[i] != nums[insertPosition - 2], so skip this 2 as it was included twice.
- // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
- // nums = [1, 1, 2, 2, 1, 2, 2, 2, 2, 2, 3, 3, 3].
- // insertPosition = 4 ^
- // i = 9 ^
- // nums[i] != nums[insertPosition - 2], so skip this 2 as it was included twice.
- // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
- // nums = [1, 1, 2, 2, *3*, 2, 2, 2, 2, 2, 3, 3, 3].
- // insertPosition = 4 ^
- // i = 10 ^
- // nums[i] != nums[insertPosition - 2], so put 3 at insertPosition = 4
- // and increment insertPosition by 1.
- // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
- // nums = [1, 1, 2, 2, 3, *3*, 2, 2, 2, 2, 3, 3, 3].
- // insertPosition = 5 ^
- // i = 11 ^
- // nums[i] != nums[insertPosition - 2], so put 3 at insertPosition = 5
- // and increment insertPosition by 1.
- // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
- // nums = [1, 1, 2, 2, 3, 3, 2, 2, 2, 2, 3, 3, 3].
- // insertPosition = 6 ^
- // i = 12 ^
- // nums[i] != nums[insertPosition - 2], so skip this 3 as it was included twice.
- // Return insertPosition = 6, as six values at indices 0..5 are the answer.
- // All values after position 6 shall be ignored like this:
- // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
- // nums = [1, 1, 2, 2, 3, 3, _, _, _, _, _, _, _].
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement