2016.6.17——Remove Duplicates from Sorted Array
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的更多相关文章
- 26. Remove Duplicates from Sorted Array
题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...
- 80 remove duplicates from sorted array 2
| 分类 leetcode | Follow up for "Remove Duplicates": What if duplicates are allowed at most ...
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- Remove Duplicates From Sorted Array
Remove Duplicates from Sorted Array LeetCode OJ Given a sorted array, remove the duplicates in place ...
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 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 ...
- LeetCode:Remove Duplicates from Sorted Array I II
LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...
- 【26】Remove Duplicates from Sorted Array
[26]Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such th ...
随机推荐
- Dubbo学习(一) Dubbo原理浅析
一.初入Dubbo Dubbo学习文档: http://dubbo.incubator.apache.org/books/dubbo-user-book/ http://dubbo.incubator ...
- POJ1637_Sightseeing tour
给一个联通图,有的是单向边,有的是双向边,问是否存在欧拉回路. 乍一看毫无思路,可以这样来搞,对于每条无向边,我们随便指定一个方向,看看是否能够做到所有点的度数之和为偶数. 接下来,对于我们指定的边, ...
- SD/MMC相关寄存器的介绍
1.SD卡内部架构 在熟悉SD/MMC相关寄存器之前,我们先来看看SD卡的内部架构是怎么样的,如下图所示: 2.SD/MMC相关寄存器的介绍 从上图中总结出:SD卡内部有7个寄存器. 一.OCR,CI ...
- 自定义smokeping告警(邮件+短信)
前段时间接到公司IT同事需求,帮助其配置smokeping的告警功能,之前配置的姿势有些问题,告警有些问题,现在调试OK,在此将关键配置点简单记录下. 关键的配置项主要有: 定义告警规则并配置将告警信 ...
- QProcess 进程调用
1. 调用方的接口: void QProcess::start(const QString &program, const QStringList &arguments, OpenMo ...
- 树形DP入门详解+题目推荐
树形DP.这是个什么东西?为什么叫这个名字?跟其他DP有什么区别? 相信很多初学者在刚刚接触一种新思想的时候都会有这种问题. 没错,树形DP准确的说是一种DP的思想,将DP建立在树状结构的基础上. 既 ...
- Day24-图片验证码----待续
一,上课所讲---------没听懂!!! 1,首先,图片验证码不能放到客户浏览器的cookie上,否则,客户可以直接从它的浏览器上拿到,然后自动提交,这样的话,验证码就没有任何用处了.所以图片验证码 ...
- IbatisNet连接oracle 报错
提示什么 connect oracle 1.5.0.xxxx 将你本机的oracle 客户端版本重装换成32位即可
- MT【118】利用线面角最小解题
解:如图将正四面体放到立方体中,让AB通过$\alpha$面,让$\alpha$面绕着AB动起来.问题就转化成为EF与面$\alpha$线面角$\theta$了.EF的投影为$|EF|cos\thet ...
- 【linux之文件查看,操作,权限管理】
一.shell如何处理命令 1.shell会根据在命令中出现的空格字符,将命令划分为多个部分 2.判断第一个字段是内部命令还是外部命令 内部命令:内置于shell的命令(shell builtin) ...