public class Solution {
public int RemoveDuplicates(int[] nums) {
var len = nums.Length;
if (len == )
{
return ;
}
else
{
var pre = nums[];
var diffindex = ;
for (int i = ; i < nums.Length; i++)
{
if (pre != nums[i])
{
nums[++diffindex] = nums[i];
pre = nums[i];
}
}
return diffindex + ;
}
}
}

python实现如下:

 class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
s = set()
n = len(nums)
count =
i,j = ,
while i < n:
if len(s) == or nums[i] not in s:
s.add(nums[i])
nums[j] = nums[i]
i +=
j +=
count +=
else:
i +=
return count

不实用额外的空间,实现如下:

 class Solution:
def removeDuplicates(self, nums: 'List[int]') -> int:
n = len(nums)
i =
j = i +
count =
while i < n:
while j < n and nums[i] == nums[j]:
j +=
count +=
if i + < n and j < n:
nums[i+] = nums[j]
i +=
else:
return count
return count

leetcode26的更多相关文章

  1. leetcode-26.删除重复数组中的重复项

    leetcode-26.删除重复数组中的重复项 题意 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数 ...

  2. Leetcode26——删除有序数组中的重复项(双指针法)

    Leetcode26--删除有序数组中的重复项(双指针法) 1. 题目简述 给你一个升序排列的数组 nums ,请你原地 删除重复出现的元素,使每个元素只出现一次 ,返回删除后数组的新长度.元素的相对 ...

  3. LeetCode26 Remove Duplicates from Sorted Array

    题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...

  4. [Swift]LeetCode26. 删除排序数组中的重复项 | Remove Duplicates from Sorted Array

    Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...

  5. 删除排序数组中的重复项-leetcode-26

    public:    int removeDuplicates(vector<int>& nums) {        int size=nums.size();        i ...

  6. leetcode26: 删除排序数组中的重复项

    给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成. 示例 1 ...

  7. leetCode26.删除排序数组中的重复项

    给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成. 示例 1 ...

  8. [array] leetCode-26. Remove Duplicates from Sorted Array - Easy

    26. Remove Duplicates from Sorted Array - Easy descrition Given a sorted array, remove the duplicate ...

  9. scala刷LeetCode--26 删除排序数组中的重复项

    一.题目描述 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完 ...

随机推荐

  1. hdu3549网络流之最大流

    Ford-Fulkerson方法:dfs实现 dfs  140ms #include<map> #include<set> #include<cmath> #inc ...

  2. Invalid bound statement (not found)错误的可能原因

    其他原因导致此问题解决参考: 1.检查xml文件所在package名称是否和Mapper interface所在的包名 <mapper namespace="me.tspace.pm. ...

  3. Python3 字典Dict(十三)

    Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度. 字典是另一种可变容器模型,且可存储任意类 ...

  4. 5.2离线使用xadmin包

    把xadmin包放到项目目录下,便于修改xadmin中的代码. 首先解压下载好的 xadmin-django2.zip 压缩包,拷贝子文件夹中的xadmin文件夹,到项目中新建extra_apps文件 ...

  5. CS231n课程笔记翻译4:最优化笔记

    译者注:本文智能单元首发,译自斯坦福CS231n课程笔记Optimization Note,课程教师Andrej Karpathy授权翻译.本篇教程由杜客翻译完成,堃堃和李艺颖进行校对修改.译文含公式 ...

  6. self-taught learning setting && semi-supervised learning

    参考文献: 摘于上文献: The more general and powerful setting is the self-taught learning setting, which does n ...

  7. JavaScript的this原理

    this原理 理解下面两种写法,可能有不一样的结果. var obj = { foo: function () {} }; var foo = obj.foo; // 写法一 obj.foo() // ...

  8. backports移植rtlwifi驱动

    /************************************************************************ * backports移植rtlwifi驱动 * 说 ...

  9. I.MX6 Manufacturing Tool V2 (MFGTool2) ucl2.xml hacking

    <!-- * Copyright (C) 2010-2013, Freescale Semiconductor, Inc. All Rights Reserved. * The CFG elem ...

  10. BZOJ4833: [Lydsy1704月赛]最小公倍佩尔数(min-max容斥&莫比乌斯反演)(线性多项式多个数求LCM)

    4833: [Lydsy1704月赛]最小公倍佩尔数 Time Limit: 8 Sec  Memory Limit: 128 MBSubmit: 240  Solved: 118[Submit][S ...