This commit is contained in:
fleyx 2023-11-28 23:48:59 +08:00
parent dbcc8ee582
commit 8ff7654633
3 changed files with 39 additions and 5 deletions

View File

@ -1,5 +1,7 @@
package com.fanxb.common;
import java.util.Arrays;
/**
* TODO
*
@ -23,7 +25,21 @@ public class Q26 {
return count;
}
public int newRemoveDuplicates(int[] nums) {
int i = 0, j = 1;
while (j < nums.length) {
if (nums[j] != nums[i]) {
nums[++i] = nums[j];
}
j++;
}
return i + 1;
}
public static void main(String[] args) {
System.out.println(new Q26().removeDuplicates(new int[]{0, 0, 1, 1, 1, 2, 2, 3, 3, 4}));
int[] arr = new int[]{0, 0, 1, 1, 1, 2, 2, 3, 3, 4};
System.out.println(new Q26().newRemoveDuplicates(arr));
System.out.println(Arrays.toString(arr));
}
}

View File

@ -24,7 +24,20 @@ public class Q27 {
return count;
}
public int removeElement1(int[] nums, int val) {
int i = 0, j = 0;
while (j < nums.length) {
if (nums[j] != val) {
nums[i++] = nums[j];
}
j++;
}
return i;
}
public static void main(String[] args) {
System.out.println(new Q27().removeElement(new int[]{0, 1, 2, 2, 3, 0, 4, 2}, 2));
int[] arr = new int[]{0, 1, 2, 2, 3, 0, 4, 2};
System.out.println(new Q27().removeElement1(arr, 2));
System.out.println(Arrays.toString(arr));
}
}

View File

@ -35,14 +35,19 @@ public class Q88 {
int[] nums1Copy = new int[m];
System.arraycopy(nums1, 0, nums1Copy, 0, m);
for (int i = 0, j = 0, k = 0; k < m + n; k++) {
if (j>=n || (i < m && nums1Copy[i] < nums2[j])) {
nums1[k] = nums1Copy[i++];
} else {
nums1[k] = nums2[j++];
}
}
}
public static void main(String[] args) {
int[] nums1 = new int[12];
int[] nums1 = new int[]{1,2,3};
int[] nums2 = new int[]{};
new Q88().merge(nums1,3,nums2,0);
System.out.println(Arrays.toString(nums1));
}
}