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. 利用ZooKeeper简单实现分布式锁

    1.分布式锁的由来: 在程序开发过程中不得不考虑的就是并发问题.在java中对于同一个jvm而言,jdk已经提供了lock和同步等.但是在分布式情况下,往往存在多个进程对一些资源产生竞争关系,而这些进 ...

  2. 基本数据类型用== 比较数值 引用类型用==比较的是jvm中的地址 比较数值用equal

    基本数据类型用== 比较数值     引用类型用==比较的是jvm中的地址 比较数值用equal

  3. YARN结构分析与工作流程

    YARN Architecture Link: http://hadoop.apache.org/docs/r2.7.2/hadoop-yarn/hadoop-yarn-site/YARN.html ...

  4. Spring面试,IoC和AOP的理解, @Transactional原理及使用

    spring 的优点?1.降低了组件之间的耦合性 ,实现了软件各层之间的解耦 2.可以使用容易提供的众多服务,如事务管理,消息服务等 3.容器提供单例模式支持 4.容器提供了AOP技术,利用它很容易实 ...

  5. A Shade of Moonlight CodeForces - 989D

    看这位博主的吧:https://blog.csdn.net/Steaunk/article/details/80660954

  6. 51nod 1483 化学变换 | 二进制 暴力

    51nod 1483 化学变换 题面 给出n个整数(n <= 1e5,每个数 <= 1e5),对每个数都可以进行多次操作,操作有两种:乘二/整除以二. 问最少经过多少次操作能使所有数相等. ...

  7. BZOJ2437 [Noi2011]兔兔与蛋蛋 【博弈论 + 二分图匹配】

    题目链接 BZOJ2437 题解 和JSOI2014很像 只不过这题动态删点 如果我们把空位置看做\(X\)的话,就会发现我们走的路径是一个\(OX\)交错的路径 然后将图二分染色,当前点必胜,当且仅 ...

  8. 20135319zl软件破解报告

    编写一个简单的C程序.要求只有输入a,才能通过. 现在,使用objdump –d po反汇编这个程序 找到main函数,可以发现movb $0x61,0x1f(%esp)这句语句中是将字符a(对应0x ...

  9. Android中用GridView实现九宫格的两种方法(转)

    Android中用GridView实现九宫格的两种方法http://blog.csdn.net/shakespeare001/article/details/7768455 1.传统办法:实现一个继承 ...

  10. SpringMVC 使用@ResponseBody返回json 中文乱码

    这确实是个蛋疼的问题,Spring中解析字符串的转换器默认编码居然是ISO-8859-1 既然找到问题了,那就必须想办法改过来,不同版本的Spring好像方法还不一样,网上不少说的都是Spring3. ...