|
1 | 1 | package com.fishercoder.solutions;
|
2 | 2 |
|
3 |
| -/** Given a sorted array, remove the duplicates |
| 3 | +/** |
| 4 | + * 26. Remove Duplicates from Sorted Array |
| 5 | + * |
| 6 | + * Given a sorted array, remove the duplicates |
4 | 7 | * in place such that each element appear only once and return the new length.
|
5 | 8 | * Do not allocate extra space for another array, you must do this in place with constant memory.
|
6 |
| -
|
7 |
| - For example, |
8 |
| - Given input array A = [1,1,2], |
9 |
| -
|
10 |
| - Your function should return length = 2, and A is now [1,2].*/ |
| 9 | + * |
| 10 | + * For example, |
| 11 | + * Given input array A = [1,1,2], |
| 12 | + * Your function should return length = 2, and A is now [1,2]. |
| 13 | + * */ |
11 | 14 |
|
12 | 15 | public class _26 {
|
13 | 16 |
|
14 |
| - public static int removeDuplicates_editorial_solution(int[] nums) { |
15 |
| - int i = 0; |
16 |
| - for (int j = 1; j < nums.length; j++) { |
17 |
| - if (nums[i] != nums[j]) { |
18 |
| - i++; |
19 |
| - nums[i] = nums[j]; |
20 |
| - } |
| 17 | + public static class Solution1 { |
| 18 | + public int removeDuplicates(int[] nums) { |
| 19 | + int i = 0; |
| 20 | + for (int j = 1; j < nums.length; j++) { |
| 21 | + if (nums[i] != nums[j]) { |
| 22 | + i++; |
| 23 | + nums[i] = nums[j]; |
21 | 24 | }
|
22 |
| - return i + 1; |
23 |
| - } |
24 |
| - |
25 |
| - |
26 |
| - public static void main(String... strings) { |
27 |
| - int[] nums = new int[]{1, 1, 2}; |
28 |
| -// int[] nums = new int[]{1,1,2,2,3}; |
29 |
| -// int[] nums = new int[]{1,1}; |
30 |
| - System.out.println(removeDuplicates_editorial_solution(nums)); |
| 25 | + } |
| 26 | + return i + 1; |
31 | 27 | }
|
| 28 | + } |
32 | 29 |
|
| 30 | + public static class Solution2 { |
33 | 31 | /**
|
34 | 32 | * Same idea as the editorial solution, mine just got more verbose.
|
35 | 33 | */
|
36 |
| - public static int removeDuplicates_my_original(int[] nums) { |
37 |
| - int i = 0; |
38 |
| - for (int j = i + 1; i < nums.length && j < nums.length; ) { |
39 |
| - while (j < nums.length && nums[i] == nums[j]) { |
40 |
| - j++; |
41 |
| - } |
42 |
| - if (j == nums.length) { |
43 |
| - j--; |
44 |
| - } |
45 |
| - int temp = nums[j]; |
46 |
| - nums[j] = nums[i + 1]; |
47 |
| - nums[i + 1] = temp; |
48 |
| - if (nums[i] != nums[i + 1]) { |
49 |
| - i++; |
50 |
| - } |
51 |
| - if (j == nums.length) { |
52 |
| - break; |
53 |
| - } |
54 |
| - j++; |
| 34 | + public static int removeDuplicates(int[] nums) { |
| 35 | + int i = 0; |
| 36 | + for (int j = i + 1; i < nums.length && j < nums.length; ) { |
| 37 | + while (j < nums.length && nums[i] == nums[j]) { |
| 38 | + j++; |
| 39 | + } |
| 40 | + if (j == nums.length) { |
| 41 | + j--; |
| 42 | + } |
| 43 | + int temp = nums[j]; |
| 44 | + nums[j] = nums[i + 1]; |
| 45 | + nums[i + 1] = temp; |
| 46 | + if (nums[i] != nums[i + 1]) { |
| 47 | + i++; |
| 48 | + } |
| 49 | + if (j == nums.length) { |
| 50 | + break; |
55 | 51 | }
|
56 |
| - return i + 1; |
| 52 | + j++; |
| 53 | + } |
| 54 | + return i + 1; |
57 | 55 | }
|
| 56 | + } |
58 | 57 |
|
59 | 58 | }
|
0 commit comments