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" ...
随机推荐
- Python中的shelve模块
shelve中有用的函数就是open(),但是下面编写的数据库函数中调用路径是经常出错,如果直接调用一个从来没有用过的文件却能正常运行,暂时没有找出原因. 调用shelve.open()会返回一个sh ...
- CRF图像切割简单介绍
这里主要是讲Conditional Random Fields(CRF)用于pixel-wise的图像标记(事实上就是图像切割).CRF经经常使用于 pixel-wise的label 预測.当把像素的 ...
- excel 学习
最近越來越感覺到熟練運用office軟件是一門很深的學問,以前一直忽視了這個技能,因爲從來都不覺得這個有什麼技術難度.正是因爲這樣想,所以才一直沒能去好好研究使用這套工具.現在卻覺得有必要學習,在於: ...
- leetcode 316. Remove Duplicate Letters
Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...
- UESTC149 解救小Q
小Q被邪恶的大魔王困在了迷宫里,love8909决定去解救她.迷宫里面有一些陷阱,一旦走到陷阱里,就会被困身亡:(,迷宫里还有一些古老的传送阵,一旦走到传送阵上,会强制被传送到传送阵的另一头.现在请你 ...
- 深入了解以太坊虚拟机第4部分——ABI编码外部方法调用的方式
在本系列的上一篇文章中我们看到了Solidity是如何在EVM存储器中表示复杂数据结构的.但是如果无法交互,数据就是没有意义的.智能合约就是数据和外界的中间体. 在这篇文章中我们将会看到Solidit ...
- Linux下安装Openfire 4.2.1
1.下载安装包,下载地址:http://www.igniterealtime.org/downloads/index.jsp#openfire 2.将下载的安装包复制到linux服务器的/opt目录下 ...
- settings.xml 文件配置
Maven 远程中央仓库:repo1.maven.org 通过 ping repo1.maven.org 查看该网络是否可以连接访问: 1. 设置代理 Plugin org.apache.maven. ...
- SELinux 初探
SELinux:Security Enhanced Linux.SELinux 是 NSA(美国国家安全局)开发设计,整合到 Linux 内核中的一个模块. 0. 基本概念 DAC(Discretio ...
- CodeForces 721B Journey (DP)
题意:给定一个有向图,你从1出发到n,走尽可能多的点,并且使总权值不大于t. 析:在比赛时,竟然看成有向图了,就想了好久,感觉dp,但是不会啊...如果是有向图就好做多了,枚举边,然后打印就好,dp[ ...