leetcoe系列之删除数组中的重复值
作者:互联网
import org.junit.Test;
public class RemoveDuplicates {
/**
* @param args
*/
@Test
public void main1() {
int[] nums = {0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 3, 3, 4};
int k = 0;
for (int j = 1; j < nums.length; j++) {
if (nums[j] != nums[k]) {
nums[++k] = nums[j];
}
}
for (int i = 0; i < k + 1; i++) {
System.out.println(nums[i]);
}
return k + 1;
}
/**
* 官方题解,自己手撸一遍
*/
@Test
public void sol() {
int[] nums = {0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 3, 3, 4};
int i = 0;
for (int j = 1; j < nums.length; j++) {
if (nums[j] != nums[i]) {
i++;
nums[i] = nums[j];
}
}
for (int k = 0; k < i + 1; k++) {
System.out.println(nums[k]);
}
}
}
标签:leetcoe,删除,nums,int,System,++,数组,Test,public 来源: https://blog.csdn.net/zhao__zhen/article/details/88389262