【LeetCode】442. Find All Duplicates in an Array 解题报告(Python& C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/find-all-duplicates-in-an-array/description/
题目描述
Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.
Find all the elements that appear twice in this array.
Could you do it without extra space and in O(n) runtime?
Example:
Input:
[4,3,2,7,8,2,3,1]
Output:
[2,3]
题目大意
在一个数组中,有些数字出现了两次,有些数字出现一次。找出出现两次的所有数字。
解题方法
字典
使用字典统计出现次数。
class Solution {
public:
vector<int> findDuplicates(vector<int>& nums) {
map<int, int> count;
for (int num : nums) {
count[num] ++;
}
vector<int> res;
for (auto c : count) {
if (c.second == 2)
res.push_back(c.first);
}
return res;
}
};
原地变负
这个题的难点在于O(n)的时间复杂度和不用额外空间。下面的做法我是按照Discuss做的,使用了题目给的一个trick,1 ≤ a[i] ≤ n
,这样使用a[i]-1把数组中该位置的元素求反,当再次遇到该位置时能够通过这个位置是负数来确定出现了两次。因为可能会对还没有扫描到的位置进行求反,所以当扫描到该位置的时候应该进行求绝对值操作。
class Solution(object):
def findDuplicates(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
ans = []
for num in nums:
if nums[abs(num) - 1] < 0:
ans.append(abs(num))
nums[abs(num) - 1] *= - 1
return ans
C++代码如下:
class Solution {
public:
vector<int> findDuplicates(vector<int>& nums) {
const int N = nums.size();
vector<int> res;
for (int i = 0; i < N; i++) {
if (nums[abs(nums[i]) - 1] < 0)
res.push_back(abs(nums[i]));
nums[abs(nums[i]) - 1] *= -1;
}
return res;
}
};
日期
2018 年 2 月 6 日
2018 年 12 月 5 日 —— 周三啦!
【LeetCode】442. Find All Duplicates in an Array 解题报告(Python& C++)的更多相关文章
- 【LeetCode】26. Remove Duplicates from Sorted Array 解题报告(Python&C++&Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 日期 [LeetCode] https:// ...
- LeetCode 442. 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 442. Find All Duplicates in an Array 查找数组中的所有重复项
https://leetcode.com/problems/find-all-duplicates-in-an-array/description/ 参考:http://www.cnblogs.com ...
- LeetCode - 442. Find All Duplicates in an Array - 几种不同思路 - (C++)
题目 题目链接 Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and ...
- 【LeetCode】Find Minimum in Rotated Sorted Array 解题报告
今天看到LeetCode OJ题目下方多了"Show Tags"功能.我觉着挺好,方便刚開始学习的人分类练习.同一时候也是解题时的思路提示. [题目] Suppose a sort ...
- 【LeetCode】83. Remove Duplicates from Sorted List 解题报告(C++&Java&Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 判断相邻节点是否相等 使用set 使用列表 递归 日 ...
- 【LeetCode】977. Squares of a Sorted Array 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcod ...
- 【LeetCode】532. K-diff Pairs in an Array 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...
- 【LeetCode】33. Search in Rotated Sorted Array 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- == 和 equals() 方法的区别
== 在比较基本数据类型时,是比较两边的数据的值是否相等 // 整数类型 int num1 = 1; // 双精度浮点数类型 double num2 = 1.0; // 输出结果为 true Syst ...
- A Child's History of England.50
'Knave [man without honor]!' said King Richard. 'What have I done to thee [you] that thou [you] shou ...
- A Child's History of England.17
CHAPTER 6 ENGLAND UNDER HAROLD HAREFOOT, HARDICANUTE, AND EDWARD THE CONFESSOR Canute left three son ...
- 商业爬虫学习笔记day4
一.获取登录后页面信息的两种方法 1.第一种方法: 人为把有效cookies加到请求头中,代码如下 import urllib.request # 确定url url = "https:// ...
- 【STM32】使用SDIO进行SD卡读写,包含文件管理FatFs(八)-认识内存管理
[STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(一)-初步认识SD卡 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(二)-了解SD总线,命令的相关介绍 [STM3 ...
- mybatis-扩展
分页插件 使用pageHelper参考官方https://github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/zh/HowToUse. ...
- OpenStack之三: 安装MySQL,rabbitmq, memcached
官网地址:https://docs.openstack.org/install-guide/environment-sql-database-rdo.html #:安装mysql [root@mysq ...
- 【Linux】【Commands】systemd
1. 系统启动流程:POST --> Boot Sequeue(BIOS) --> Bootloader(MBR) --> Kernel(ramdisk) --> rootfs ...
- 第7章 使用性能利器——Redis
在现今互联网应用中,NoSQL已经广为应用,在互联网中起到加速系统的作用.有两种NoSQL使用最为广泛,那就是Redis和MongoDB.本章将介绍Redis和Spring Boot的结合.Redis ...
- sql优化的8种方式
1.设置索引. MySQL索引操作:给表列创建索引: 建表时创建索引: create table t(id int,name varchar(20),index idx_name (name)); 给 ...