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 ofnums
being1
and2
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 ofnums
being modified to0
,1
,2
,3
, and4
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的更多相关文章
- [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- ...
随机推荐
- CentOS下查看文件和文件夹大小
当磁盘大小超过标准时会有报警提示,这时如果掌握df和du命令是非常明智的选择. df可以查看一级文件夹大小.使用比例.档案系统及其挂入点,但对文件却无能为力. 当磁盘大小超过标准时会有报警提示,这时如 ...
- tkinter比较常用的组件
1.输入框组件 输入框(Entry)用来输入单行内容,可以方便地向程序传递用户参数.这里通过一个转换摄氏度和华氏度的小程序来演示该组件的使用. import tkinter as tk def btn ...
- HDU 4034 Graph:反向floyd
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4034 题意: 有一个有向图,n个节点.给出两两节点之间的最短路长度,问你原图至少有多少条边. 如果无解 ...
- jquery中bind,live,delegate,on的区别
这几种方法都是绑定事件用到的,但是他们之间有些差别 bind(type,[data],fn) 为每个匹配元素的特定事件绑定事件处理函数 例如: <ul> <a href=" ...
- python--17个新手常见Python运行时错误
当初学 Python 时,想要弄懂 Python 的错误信息的含义可能有点复杂.这里列出了常见的的一些让你程序 crash 的运行时错误. 1)忘记在 if , elif , else , for , ...
- CTS 2019 Pearl
CTS2019 Pearl 每种颜色有奇偶两种情况,只有偶数和只有奇数个,其对应的的指数型生成函数分别为 \(\frac{e^x+e^{-x}}2,\frac{e^x-e^{-x}}2\) 考虑选出现 ...
- JQuery基本知识、选择器、事件、DOM操作、动画--2017年2月10日
$(对象)可以将JS对象转换为JQuery对象 .get(0)可以将JQuery对象转换为JS对象 并无太大区别,灵活点出即可
- hdu 3998 Sequence
There is a sequence X (i.e. x[1], x[2], ..., x[n]). We define increasing subsequence of X as x[i1], ...
- Codeforces Round #402 (Div. 2) 题解
Problem A: 题目大意: 给定两个数列\(a,b\),一次操作可以交换分别\(a,b\)数列中的任意一对数.求最少的交换次数使得任意一个数都在两个序列中出现相同的次数. (\(1 \leq a ...
- 洛谷 P4546 & bzoj 5020 在美妙的数学王国中畅游 —— LCT+泰勒展开
题目:https://www.luogu.org/problemnew/show/P4546 先写了个55分的部分分,直接用LCT维护即可,在洛谷上拿了60分: 注意各处 pushup,而且 spla ...