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. TStringGrid的Rows索引值 和 Cells的 索引值, Row的赋值

    Caption := sgShopList.Rows[sgShopList.RowCount +].CommaText; Caption := sgShopList.Rows[sgShopList.R ...

  2. 线程的同步之Synchronized的使用

       一.介绍        线程的同步:一般的并发指的就是多个线程访问同一份资源.多个线程同时访问(修改)同一份资源的话,就会有可能造成资源数据有误. 如果多个线程访问多个不同资源,就不会造成线程同 ...

  3. Jenkins插件开发(四)-- 插件发布

    上一篇blog介绍了插件开发中要注意的一些问题, 我们再来介绍插件开发完成后,如何上传到jenkins的插件中心(这里假设你的代码是放在github上的,使用svn或其他版本管理工具的请参考其他文章) ...

  4. 使用百度地图SDK出现的问题及解决方法

    1. 第一个错误信息如下: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.baiduma ...

  5. 2019年微信小程序1月TOP100榜单

  6. openGL之着色器程序的使用

    #define GLEW_STATIC #include <GL\glew.h> #include <GLFW\glfw3.h> #include<iostream> ...

  7. php 递归读取目录

    看到很多面试题有这个,今天有机会写了一下. 要注意的是: 在opendir这个函数用完后,要注意closedir,因为安全问题,打开的目录依然存在于内存中,在并发情况下最好关闭,不然容易被破坏. &l ...

  8. elasticsearch的插件安装

    目前使用的是2.4.5版本的es 安装的时候注意以下几点 : 1.如果想所有的ip都能访问es,需要修改config下的elasticsearch.yml.修改如下 network.host=0.0. ...

  9. 在WPF中使用CefSharp嵌入浏览器(转)

    在WPF中使用CefSharp嵌入浏览器   日常开发中,我们需要将一些Web页面嵌入到桌面客户端软件中.下面我们使用CefSharp嵌入浏览器来实现. 首先先介绍一下CefSharp嵌入式浏览器,它 ...

  10. Java 的Integer、int与new Integer到底怎么回事?

    先做一些总结,询问了些经验比较多的师傅,在这里表示感谢,然后自己总结下,今天的收获分享给大家: 1. int 和Integer在进行比较的时候,Integer会进行拆箱,转为int值与int进行比较. ...