Day by Day Leetcode Challenge
leetcode 2和c leetcode_day_by_day (Swift) 第1天:从排序数组中删除重复项。给定一个已排序的数组,删除原地重复项,使每个元素只出现一次并返回新的长度。
C代码示例:
int removeDuplicates(int* nums, int numsSize) {
if (numsSize == 0) return 0;
int index = 1;
for (int i = 1; i < numsSize; i++) {
if (nums[i] != nums[index-1])
nums[index++] = nums[i];
}
return index;
}
Swift代码示例:
func removeDuplicates(_ nums:[Int], _ numsSize: Int) -> Int {
var nums = nums
if numsSize == 0 { return 0 }
var index = 1
for i in 1.. if nums[i] != nums[index - 1] { nums[index] = nums[i] index += 1 } } return index }