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个就为不重复的。

  代码

  1. class MyClass
  2. {
  3. public:
  4. int removeDuplicate(vector<int> nums)
  5. {
  6. int j = , n = ;
  7. n = nums.size();
  8. for (size_t i = ; i < n; i++)
  9. {
  10. if (nums[i - ] == nums[i])
  11. {
  12. j++;
  13. }
  14. else
  15. nums[i - j] = nums[i]; //亮点所在
  16. }
  17. return n-j;
  18. }

  我的测试代码:

  

  1. // Remove Duplicates from Sorted Array.cpp : 定义控制台应用程序的入口点。
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "iostream"
  6. #include "vector"
  7.  
  8. using namespace std;
  9.  
  10. class MyClass
  11. {
  12. public:
  13. int removeDuplicate(vector<int> nums)
  14. {
  15. int j = , n = ;
  16. n = nums.size();
  17. for (size_t i = ; i < n; i++)
  18. {
  19. if (nums[i - ] == nums[i])
  20. {
  21. j++;
  22. }
  23. else
  24. nums[i - j] = nums[i];
  25. }
  26. return n-j;
  27. }
  28. };
  29.  
  30. int _tmain(int argc, _TCHAR* argv[])
  31. {
  32. vector<int> nums = { , , , , , , };
  33. MyClass solution;
  34. vector<int> m;
  35. int n = ;
  36. n = solution.removeDuplicate(nums);
  37. cout << n << endl;
  38. //测试输出数组用
  39. /*
  40. for (size_t i = 0; i < m.size();i++)
  41. {
  42. cout << m[i] << " ";
  43. }
  44. cout << endl;*/
  45. system("pause");
  46. return ;
  47. }

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. SpringBoot(八)_springboot集成swagger2

    swagger是一个功能强大的api框架,它的集成非常简单,不仅提供了在线文档的查阅,而且还提供了在线文档的测试. (1) 引入依赖,我们选择现在最新的版本 <dependency> &l ...

  2. jdk10配置

    解压 vi /etc/profile JAVA_HOME=/home/elasticsearch/jdk- CLASSPATH=$JAVA_HOME/lib/ PATH=$PATH:$JAVA_HOM ...

  3. BZOJ5254 FJWC2018红绿灯(线段树)

    注意到一旦在某个路口被红灯逼停,剩下要走的时间是固定的.容易想到预处理出在每个路口被逼停后到达终点的最短时间,这样对于每个询问求出其最早在哪个路口停下就可以了.对于预处理,从下一个要停的路口倒推即可. ...

  4. How Many Points? LightOJ - 1077(线段经过整点个数与gcd 证明)

    题意: 已知两点 (x1,y1) 和 (x2, y2)求两点间线段上的整点的个数 解析: 就是求gcd(abs(x2- x1),abs(y2 - y1)) 证明: 我们分水平方向和竖直方向两个方向看 ...

  5. Crawl(2)

    http://cuiqingcai.com/3179.html # *-* coding: UTF-8 *-* import urllib2 import cookielib import re im ...

  6. 【刷题】BZOJ 3732 Network

    Description 给你N个点的无向图 (1 <= N <= 15,000),记为:1-N. 图中有M条边 (1 <= M <= 30,000) ,第j条边的长度为: d_ ...

  7. 【刷题】洛谷 P1501 [国家集训队]Tree II

    题目描述 一棵n个点的树,每个点的初始权值为1.对于这棵树有q个操作,每个操作为以下四种操作之一: + u v c:将u到v的路径上的点的权值都加上自然数c: - u1 v1 u2 v2:将树中原有的 ...

  8. 【刷题】洛谷 P3768 简单的数学题

    题目描述 由于出题人懒得写背景了,题目还是简单一点好. 输入一个整数n和一个整数p,你需要求出(\(\sum_{i=1}^n\sum_{j=1}^n ijgcd(i,j))~mod~p\),其中gcd ...

  9. 【NOI】荷马史诗

    追逐影子的人,自己就是影子 ——荷马 Allison最近迷上了文学.她喜欢在一个慵懒的午后,细细地品上一杯卡布奇诺,静静地阅读她爱不释手的<荷马史诗>.但是由<奥德赛>和< ...

  10. scala 的安装 与 IDEA安装使用

    一.安装 scala 1.下载scala-2.11.8.msi 安装包,   首先去官网http://www.scala-lang.org/,然后点击导航栏的DOWNLOAD,进入下载链接:http: ...