442 Find All Duplicates in an Array 数组中重复的数据
给定一个整数数组 a,其中1 ≤ a[i] ≤ n (n为数组长度), 其中有些元素出现两次而其他元素出现一次。
找到所有出现两次的元素。
你可以不用到任何额外空间并在O(n)时间复杂度内解决这个问题吗?
示例:
输入:
[4,3,2,7,8,2,3,1]
输出:
[2,3]
详见:https://leetcode.com/problems/find-all-duplicates-in-an-array/description/
C++:
方法一:
class Solution {
public:
vector<int> findDuplicates(vector<int>& nums) {
vector<int> res;
for(int i=0;i<nums.size();++i)
{
int idx=abs(nums[i])-1;
if(nums[idx]<0)
{
res.push_back(idx+1);
}
nums[idx]=-nums[idx];
}
return res;
}
};
方法二:
class Solution {
public:
vector<int> findDuplicates(vector<int>& nums)
{
vector<int> res;
for (int i = 0; i < nums.size(); ++i)
{
if (nums[i] != nums[nums[i] - 1])
{
swap(nums[i], nums[nums[i] - 1]);
--i;
}
}
for (int i = 0; i < nums.size(); ++i)
{
if (nums[i] != i + 1)
{
res.push_back(nums[i]);
}
}
return res;
}
};
参考:https://www.cnblogs.com/grandyang/p/6209746.html
442 Find All Duplicates in an Array 数组中重复的数据的更多相关文章
- LeetCode 442. 数组中重复的数据(Find All Duplicates in an Array) 17
442. 数组中重复的数据 442. Find All Duplicates in an Array 题目描述 Given an array of integers, 1 ≤ a[i] ≤ n (n ...
- Leetcode#442. Find All Duplicates in an nums(数组中重复的数据)
题目描述 给定一个整数数组 a,其中1 ≤ a[i] ≤ n (n为数组长度), 其中有些元素出现两次而其他元素出现一次. 找到所有出现两次的元素. 你可以不用到任何额外空间并在O(n)时间复杂度内解 ...
- Java实现 LeetCode 442 数组中重复的数据
442. 数组中重复的数据 给定一个整数数组 a,其中1 ≤ a[i] ≤ n (n为数组长度), 其中有些元素出现两次而其他元素出现一次. 找到所有出现两次的元素. 你可以不用到任何额外空间并在O( ...
- leetcode 26 80 删除已排序数组中重复的数据
80. Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if dupli ...
- [Swift]LeetCode442. 数组中重复的数据 | Find All Duplicates in an Array
Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others ...
- leetcode 217. Contains Duplicate 287. Find the Duplicate Number 442. Find All Duplicates in an Array 448. Find All Numbers Disappeared in an Array
后面3个题都是限制在1-n的,所有可以不先排序,可以利用巧方法做.最后两个题几乎一模一样. 217. Contains Duplicate class Solution { public: bool ...
- 【LeetCode每天一题】Remove Duplicates from Sorted Array II(移除有序数组中重复的两次以上的数字)
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- 442. Find All Duplicates in an Array - LeetCode
Question 442. Find All Duplicates in an Array Solution 题目大意:在数据中找重复两次的数 思路:数组排序,前一个与后一个相同的即为要找的数 Jav ...
- php去除数组中重复数据
<?php /** * 去除数组中重复数据 * by www.jbxue.com **/ $input = array("a" => "green" ...
随机推荐
- webservice0基础
在学习webservice的时候,常常将ns和url花了好久时间才理解过来,这里备份下. 首先定义接口: @WebService public interface IService { @WebRes ...
- Real-Time Compressive Tracking 论文笔记
总体思想 1 利用符合压缩感知RIP条件的随机感知矩阵对多尺度图像进行降维 2 然后对降维的特征採用简单的朴素贝叶斯进行分类 算法主要流程 1 在t帧的时候,我们採样得到若干张目标(正样本)和背景(负 ...
- Python开发【1.1 基础语法】
1.Python语言特点 优点: ①.丰富的库 ②.简单.开源 ③.支持面向对象编程 ④.解释性语言,无需编译 ⑤.高层语言,不用考虑内存问题 ⑥.可移植性好,不依赖于操作系统 缺点: ①.运行效率较 ...
- Why was 80 Chosen as the Default HTTP Port and 443 as the Default HTTPS Port?
https://www.howtogeek.com/233383/why-was-80-chosen-as-the-default-http-port-and-443-as-the-default-h ...
- ABAP ODATA接口开发
1.SE37 创建好 Remote-Enabled函数. 2.SEGW,新建项目 创建项目之后,展开,右键Data Model,Import 函数. 导入函数之后,创建mapping 3./IWFND ...
- scikit-learn(1) 第一个例子说明
第一个 scikit-learn例子 ................................................................................. ...
- ubuntu切换中英文通用方法,ubuntu中文语言
1:点击桌面右上角的齿轮,选择“system settings”进入系统设置界面
- MTK 电池曲线配置【转】
本文转载自:http://blog.csdn.net/u010245383/article/details/51006597 硬件平台:MT8382 网上有很多关于电池曲线配置的文档,定位到具体的文件 ...
- 修改phpmyadmin中的默认超时时间
phpmyadmin在使用过程中经常出现"登陆超时(1440秒未活动),请重新登录",很烦 解决方法如下: 第一步: 修改php.ini,找到 session.gc_maxlife ...
- Linux 下的静态(函数)库、动态(函数)库
0. 基本 在命名上,静态库的名字一般是 libxxx.a,动态库的名字一般是 libxxx.so,有时 libxxx.so.major.minor,xxx 是该 lib 的名字,major 是主版本 ...