C# 写 LeetCode easy #26 Remove Duplicates from Sorted Array
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 ofnumsbeing1and2respectively. 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 ofnumsbeing modified to0,1,2,3, and4respectively. 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的更多相关文章
- [Leetcode][Python]26: Remove Duplicates from Sorted Array
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 26: Remove Duplicates from Sorted Array ...
- 【LeetCode】26. Remove Duplicates from Sorted Array 解题报告(Python&C++&Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 日期 [LeetCode] https:// ...
- 【leetcode】 26. Remove Duplicates from Sorted Array
@requires_authorization @author johnsondu @create_time 2015.7.22 18:58 @url [remove dublicates from ...
- 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 ...
- 【一天一道LeetCode】#26. Remove Duplicates from Sorted Array
一天一道LeetCode系列 (一)题目 Given a sorted array, remove the duplicates in place such that each element app ...
- 【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 ...
- 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 ...
- LeetCode:26. Remove Duplicates from Sorted Array(Easy)
1. 原题链接 https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/ 2. 题目要求 给定一个已 ...
- 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- ...
随机推荐
- 用用OGRE1.74搭建游戏框架(转载)
用OGRE1.74搭建游戏框架(一) 新版的OGRE出来了,不知什么原因抛弃了CEGUI.国内的教程好像也更新比较少了,在官网上还是发现不少资料的,现在参考官网上的一些资料来搭建一个游戏的框架. 参考 ...
- 本地文件读取(csv,txt)时字符编码问题解决
今天进行csv文件读取时,老是入库为空,因为其中有中文字符,我要通过中文字符映射成相应的编号(上升:1011,下降:1012),于是怎么也取不到编号.刚开始以为程序映射出了问题,最后日志打出来后,发现 ...
- Codeforces 180C Letter:dp
题目链接:http://codeforces.com/problemset/problem/180/C 题意: 给你一个字符串s,长度为n. 让你将这个字符串变成“前面一段都是大写字母,后面一段都是小 ...
- spring学习(2)
理解反向控制(IOC) 依赖注入(di):比IOC更好地名字.获得依赖对象的方式反转了. IOC应用 IOC或者di,还可以达到解耦的目的. spring开发提倡接口编程,配合di技术,可以更好地达到 ...
- Eclipse 下配置MySql5.6的连接池,使用Tomcat7.0
目前找到的最简单的配置方法. 1.首先在eclipse中创建一个Dynamical Web Application,在WebContent文件夹下的META-INF文件夹中创建新的名为conten ...
- 计算地球上两个坐标点(经度,纬度)之间距离sql函数
go --计算地球上两个坐标点(经度,纬度)之间距离sql函数 --作者:lordbaby --整理:www.aspbc.com CREATE FUNCTION [dbo].[fnGetDistanc ...
- 十一 Django框架,Session
Django中默认支持Session,其内部提供了5种类型的Session供开发者使用: 1.数据库(默认)2.缓存3.文件4.缓存+数据库5.加密cookie 1.数据库Session,保存在数据库 ...
- JQuery表单验证插件EasyValidator
本插件的宗旨是:用户无需写一行JS验证代码,只需在要验证的表单中加入相应的验证属性即可,让验证功能易维护,可扩展,更容易上手. DEMO中已经包含了常用的正则表达式,可以直接复用,为了考虑扩展性,所以 ...
- inux命令学习笔记(5):rm 命令
学习了创建文件和目录的命令mkdir ,今天学习一下linux中删除文件和目录的命令: rm命令. rm是常用的命令,该命令的功能为删除一个目录中的一个或多个文件或目录,它也可以将某个目 录及其下的所 ...
- [原]NYOJ-小光棍数-458
大学生程序代写 /http://acm.nyist.net/JudgeOnline/problem.php?pid=458 *题目458题目信息运行结果本题排行讨论区小光棍数 时间限制:1000 ms ...