26、Remove Duplicates from Sorted Array

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

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

Example 1:

Given nums = [1,1,2],

Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.

It doesn't matter what you leave beyond the returned length.

Example 2:

Given nums = [0,0,1,1,1,2,2,3,3,4],

Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively.

It doesn't matter what values are set beyond the returned length.

Clarification:

Confused why the returned value is an integer but your answer is an array?

Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.

Internally you can think of this:

// nums is passed in by reference. (i.e., without making a copy)
int len = removeDuplicates(nums); // any modification to nums in your function would be known by the caller.
// using the length returned by your function, it prints the first len elements.
for (int i = 0; i < len; i++) {
    print(nums[i]);
} 代码:
static void Main(string[] args)
{
int[] nums = new int[] { ,,,,};
int res = RemoveDuplicatesfromSortedArray(nums);
Console.WriteLine(res);
Console.ReadKey();
} private static int RemoveDuplicatesfromSortedArray(int[] nums)
{
if (nums == null || nums.Length == ) return ;
int index = ;
for (int i = ; i < nums.Length; i++)
{
if (nums[i] != nums[index])
{
index++;
nums[index] = nums[i];
}
}
return index+;
}

解析:

输入:整型数组

输出:不重复的个数

思想

  首先,对空数组或数组长度为0的输出结果为0。

  其次,对数组中从第二个数开始和前面的数进行比较(这里前面的数是nums[index],即去重后的最后一个数。开始时是第一个数),若不同,则将其存入到index索引下。

  最后,由于第一个数之前没有计入,所以要加1。

时间复杂度:O(n)

C# 写 LeetCode easy #26 Remove Duplicates from Sorted Array的更多相关文章

  1. [Leetcode][Python]26: Remove Duplicates from Sorted Array

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 26: Remove Duplicates from Sorted Array ...

  2. 【LeetCode】26. Remove Duplicates from Sorted Array 解题报告(Python&C++&Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 日期 [LeetCode] https:// ...

  3. 【leetcode】 26. Remove Duplicates from Sorted Array

    @requires_authorization @author johnsondu @create_time 2015.7.22 18:58 @url [remove dublicates from ...

  4. LeetCode Array Easy 26.Remove Duplicates from Sorted Array 解答及疑惑

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

  5. 【一天一道LeetCode】#26. Remove Duplicates from Sorted Array

    一天一道LeetCode系列 (一)题目 Given a sorted array, remove the duplicates in place such that each element app ...

  6. 【LeetCode】26. Remove Duplicates from Sorted Array

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

  7. LeetCode OJ 26. Remove Duplicates from Sorted Array

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

  8. LeetCode:26. Remove Duplicates from Sorted Array(Easy)

    1. 原题链接 https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/ 2. 题目要求 给定一个已 ...

  9. Leetcode No.26 Remove Duplicates from Sorted Array(c++实现)

    1. 题目 1.1 英文题目 Given an integer array nums sorted in non-decreasing order, remove the duplicates in- ...

随机推荐

  1. 算法(Algorithms)第4版 练习 1.3.32

    ADT: /** * see if Steque is empty * @return {@code true} Steque is empty * {@code false} Steque isn' ...

  2. Java大坑之Integer对象比较相等

    public class IntegerTest { /*** * 对于Integer var = ? 在 -128到127之间的复制,Integer对象是在IntegerCache.cache产生 ...

  3. 未测试 Delphi读写UTF-8、Unicode格式文本文件

    // UTF-8文件写入函数 procedure SaveUTFFile(const FileName: string; S: string; WriteHeader: Boolean = True) ...

  4. linux应用之vim的常用命令

    vim 选择文本,删除,复制,粘贴 文本的选择,对于编辑器来说,是很基本的东西,也经常被用到,总结如下: v 从光标当前位置开始,光标所经过的地方会被选中,再按一下v结束. V 从光标当前行开始,光标 ...

  5. Appium-xpath详解

    一.xpath简介 XPath就是XML 路径,练习XPath的使用可以直接使用火狐浏览器 火狐浏览器下载 free bug和free path两个插件. 手机xpath可以自己写,路径关键字选cla ...

  6. spring MVC basic

    1.MVC&&Spring MVC .mvc的就核心思想是业务数据抽取同业务数据呈现相分离 .View,视图层,为用户提供UI,重点关注数据的呈现 .model,业务数据的信息表示,关 ...

  7. 计算机丨浏览器访问出现DNS_PROBE_POSSIBLE解决方法

    方法1.打开命令控制台输入: netsh winsock reset.提示重启,电脑重启后就ok了. 其他方法待续......

  8. (转)通过汇编语言实现C协程

    转自:http://www.cnblogs.com/sniperHW/archive/2012/06/19/2554574.html 协程的概念就不介绍了,不清楚的同学可以自己google,windo ...

  9. 在开发环境中,自己搭建一个ssl环境(小例子)

    做项目的时候自己总结的一些小例子 public class Test { public static void setSSLProperty() { Security.addProvider(new  ...

  10. codeforces 710A A. King Moves(水题)

    题目链接: A. King Moves 题意: 给出king的位置,问有几个可移动的位置; 思路: 水题,没有思路; AC代码: #include <iostream> #include ...