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. Java机器学习框架deeplearing4j入门教程

    1.添加项目maven添加依赖 or 导入jar包 or 使用jvm <project xmlns="http://maven.apache.org/POM/4.0.0" x ...

  2. iOS 数据库sqlite完整增删改查操作

    1: 创建数据库表格 1.1 — 表格创建使用一个数据库软件快速创建:软件大小14.3M; 下载地址:http://pan.baidu.com/s/1qWOgGoc; 表格创建-> 打开软件,点 ...

  3. ava8并发教程:Threads和Executors

    原文地址  原文作者:Benjamin Winterberg 译者:张坤 欢迎阅读我的Java8并发教程的第一部分.这份指南将会以简单易懂的代码示例来教给你如何在Java8中进行并发编程.这是一系列教 ...

  4. MT【118】利用线面角最小解题

    解:如图将正四面体放到立方体中,让AB通过$\alpha$面,让$\alpha$面绕着AB动起来.问题就转化成为EF与面$\alpha$线面角$\theta$了.EF的投影为$|EF|cos\thet ...

  5. div布局小技巧

    第一: 多个div整齐排列在外层div中,如图: 看到所有小的div的前后左右间隔都相等.假定已经制作好上述单元div控件.在外层大div中循环开始创建它们. for (var i=0; i < ...

  6. ZJOI2015地震后的幻想乡

    题面链接 洛咕 sol %%%_rqy 本来想写正常的状压,看到这篇题解就入坑了... 直接搬题解吧,写的太好了不用解释. 慢慢搬,先咕着QAQ #include<cstdio> #inc ...

  7. Linux中的防火墙----iptables

    防火墙,它是一种位于内部网络与外部网络之间的网络安全系统.一项信息安全的防护系统,依照特定的规则,允许或是限制传输的数据通过. 防火墙根据主要的功能可分为网络层防火墙.应用层防火墙.数据库防火墙. 网 ...

  8. kibana使用(ELK)、Lucene 查询语法

    Lucene查询 Lucene查询语法以可读的方式书写,然后使用JavaCC进行词法转换,转换成机器可识别的查询. 下面着重介绍下Lucene支持的查询: Terms词语查询 词语搜索,支持 单词 和 ...

  9. 【bzoj3926】【Zjoi2015】诸神眷顾的幻想乡

    题解: 如果树上某个路径串的端点不是叶子,那么一定是另一个串的子串: 这样只对叶子$dfs$把$20*20$个串插入广义$SAM$就是统计本质不同的串的个数的模板了: 我不太会分析广义$SAM$的空间 ...

  10. CentOS 7.0源码包搭建LNMP方法分享(实际环境下)

    CentOS 7.0编译安装Nginx1.6.0+MySQL5.6.19+PHP5.5.14 一.配置防火墙,开启80端口.3306端口 CentOS 7.0默认使用的是firewall作为防火墙,这 ...