0
点赞
收藏
分享

微信扫一扫

【Leetcode】Remove Duplicates from Sorted Array

舍予兄 2023-07-26 阅读 46


题目链接:https://leetcode.com/problems/remove-duplicates-from-sorted-array/

题目:

once

Do not allocate extra space for another array, you must do this in place with constant memory.

For example,

Given input array nums = [1,1,2],2, with the first two elements of nums being 1 and 2

思路:

思路跟 一样。

算法:

public int removeDuplicates(int[] nums) {
		int count = 0;
		for (int i = 1; i < nums.length; i++) {
			if (nums[i] == nums[i -1]) {
				count++;
			}
			nums[i - count] = nums[i];
		}
		return nums.length - count;
	}




举报

相关推荐

0 条评论