题目链接
不想戳的看下图
解题思路
循环嵌套
对于这道题,有很多种方法。首先想到的,应是循环嵌套。直接for循环两边寻找相同,并求得丢失的整数。代码就不展示了,想必大家都会写。
但时间复杂度O(n2),是相当高的,对于这题的数据范围,不适合用这个方法。
map
我们想到,可以使用STL里面的map。
代码如下:
class Solution {
public int[] findErrorNums(int[] nums) {
int result[] = new int[2];
Map<Integer, Integer> numMap = new HashMap<>();
int len = nums.length;
for (int i = 0; i < len; i