本文主要是介绍#力扣:26. 删除有序数组中的重复项@FDDLC,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
26. 删除有序数组中的重复项
一、Java
class Solution {public int removeDuplicates(int[] nums) {int l = 0;for (int i = 0; i < nums.length; nums[l++] = nums[i++]) {while (i + 1 < nums.length && nums[i] == nums[i + 1]) i++;}return l;}
}
class Solution {public int removeDuplicates(int[] nums) {int slow = 0;for (int fast = 1; fast < nums.length; fast++) {if (nums[slow] != nums[fast]) nums[++slow] = nums[fast];}return slow + 1;}
}
二、C++
#include <vector>
using namespace std;
class Solution {
public:int removeDuplicates(vector<int>& nums) {int slow = 0;for (int fast = 1; fast < nums.size(); fast++) {if (nums[slow] != nums[fast]) nums[++slow] = nums[fast];}return slow + 1;}
};
三、Python
from typing import List
class Solution:def removeDuplicates(self, nums: List[int]) -> int:slow = 0for fast in range(1, len(nums)):if nums[slow] != nums[fast]:slow += 1nums[slow] = nums[fast]fast += 1return slow + 1
四、JavaScript
var removeDuplicates = function (nums) {let slow = 0;for (let fast = 1; fast < nums.length; fast++) {if (nums[slow] != nums[fast]) nums[++slow] = nums[fast];}return slow + 1;
};
五、Go
package mainfunc removeDuplicates(nums []int) int {slow := 0for fast := 1; fast < len(nums); fast++ {if nums[slow] != nums[fast] {slow++nums[slow] = nums[fast]}}return slow + 1
}
这篇关于#力扣:26. 删除有序数组中的重复项@FDDLC的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!