题解 | #草原上的牛群#
草原上的牛群
https://www.nowcoder.com/practice/0661aa40ac8e48f4906df7aa24c3db90
考察双指针遍历的知识点,左右指针从0,1位置开始遍历,当所指位置不一致的时候,将值拷贝过去,随后左指针才往后移动,这中间右指针一直向后寻找。
完整的Java代码如下
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param nums int整型一维数组
* @return int整型
*/
public int remove_duplicates (int[] nums) {
// write code here
int left = 0;
int right = 1;
if (nums.length == 0) {
return 0;
}
while (right < nums.length) {
if (nums[left] != nums[right]) {
nums[++left] = nums[right];
}
right++;
}
return left + 1;
}
}
