Remove Duplicates from Sorted Array

本题收获:

1.“删除”数组中元素

2.数组输出

  题目:

  Given a sorted array, 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 in place with constant memory.

  For example,
  Given input array 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 new length.

  题目的意思是:返回新的数组不重复的个数,且前输出这些数(但是不是要删除重复的数,重复的数赋值放在后面),如果输出的个数正确,但是输出的数组的数不对,也会报错

  思路:

  我的思路:不知道怎么删除数组

  leetcode:将后面的不重复的值前移,重复的值赋值为最后一个值,这样输出的前length个就为不重复的。

  代码

 class MyClass
{
public:
int removeDuplicate(vector<int> nums)
{
int j = , n = ;
n = nums.size();
for (size_t i = ; i < n; i++)
{
if (nums[i - ] == nums[i])
{
j++;
}
else
nums[i - j] = nums[i]; //亮点所在
}
return n-j;
}

  我的测试代码:

  

 // Remove Duplicates from Sorted Array.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include "iostream"
#include "vector" using namespace std; class MyClass
{
public:
int removeDuplicate(vector<int> nums)
{
int j = , n = ;
n = nums.size();
for (size_t i = ; i < n; i++)
{
if (nums[i - ] == nums[i])
{
j++;
}
else
nums[i - j] = nums[i];
}
return n-j;
}
}; int _tmain(int argc, _TCHAR* argv[])
{
vector<int> nums = { , , , , , , };
MyClass solution;
vector<int> m;
int n = ;
n = solution.removeDuplicate(nums);
cout << n << endl;
//测试输出数组用
/*
for (size_t i = 0; i < m.size();i++)
{
cout << m[i] << " ";
}
cout << endl;*/
system("pause");
return ;
}

2016.6.17——Remove Duplicates from Sorted Array的更多相关文章

  1. 26. Remove Duplicates from Sorted Array

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

  2. 80 remove duplicates from sorted array 2

    | 分类 leetcode  | Follow up for "Remove Duplicates": What if duplicates are allowed at most ...

  3. [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  4. [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项

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

  5. Remove Duplicates From Sorted Array

    Remove Duplicates from Sorted Array LeetCode OJ Given a sorted array, remove the duplicates in place ...

  6. 【leetcode】Remove Duplicates from Sorted Array II

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  7. 50. Remove Duplicates from Sorted Array && Remove Duplicates from Sorted Array II && Remove Element

    Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...

  8. LeetCode:Remove Duplicates from Sorted Array I II

    LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...

  9. 【26】Remove Duplicates from Sorted Array

    [26]Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such th ...

随机推荐

  1. springcloud微服务实战:Eureka+Zuul+Feign/Ribbon+Hystrix Turbine+SpringConfig+sleuth+zipkin

    相信现在已经有很多小伙伴已经或者准备使用springcloud微服务了,接下来为大家搭建一个微服务框架,后期可以自己进行扩展.会提供一个小案例: 服务提供者和服务消费者 ,消费者会调用提供者的服务,新 ...

  2. mysql 读写锁

    1. 表读锁 lock table tablename read; 例如: 从上图中可以看到,当给表a加了读锁之后,该进程本身对表a是可读的,但是不可写,再看在另外一个进程中: 在另外一个进程中表a也 ...

  3. jmeter发送json数据,报405、400错误解决方案

    1.405错误解决方案:添加HTTP信息头管理器(错误因数:发送格式未设置) 2.400错误解决方案:json文本格式有误(注意:换行.空格等)解决方案:对照json文本数据(错误因数:发送的json ...

  4. [洛谷P5216]DLS 采花

    题目大意:有$n$个数,任意排列,排列后第$i$个数会产生贡献当且仅当$1\sim i-1$中的数不是它的因子,问所有排列的贡献和 题解:发现一个数要产生贡献要求所有它的因子在它的右边,设有$cnt_ ...

  5. BZOJ 2157: 旅游

    2157: 旅游 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 1347  Solved: 619[Submit][Status][Discuss] ...

  6. 解题:POI 2015 PUS

    题面 还以为是差分约束,原来拓扑排序也能解决这样的问题=.= 类似差分约束的建图方式,我们把大小关系看做有向边.这样一来图上是不允许存在环的,于是我们可以做拓扑排序.然后问题来了,边数非常大,根本建不 ...

  7. 【纪中集训2019.3.12】Mas的仙人掌

    题意: ​ 给出一棵\(n\)个点的树,需要加\(m\)条边,每条边脱落的概率为\(p_{i}\) ,求加入的边在最后形成图中仅在一个简单环上的边数的期望: \(1 \le n \ , m \le 1 ...

  8. 关于2-SAT

    其实以前写过关于$2-SAT$的,但是那时的自己太懵懂了. 这是以前写过的文章link 关于$2-SAT$,其实就想说两件事情. $2-SAT$边建立的逻辑 $2-SAT$边建立的逻辑是必须关系,若$ ...

  9. C++下实现同接口下多个类作为参数的调用和传参

    /* 实现同接口下不同类的对象的转移 定义类的接口 定义多个继承该接口的类 定义管理类,把接口当作类型, 传入该接口下各种类的对象,进行操作 */ #include<iostream> # ...

  10. 在VMware安装Ubuntu后一直停留在VMware Easy Install

    在VMware安装Ubuntu完成后,一直停留在VMware Easy Install,可以登录但是没有窗口界面,如图: 在此登录后依次运行以下命令: sudo mv /etc/issue.backu ...