Cover image for leetcode每日一题 P3289 数字小镇中的捣蛋鬼

leetcode每日一题 P3289 数字小镇中的捣蛋鬼

字数 188
阅读
访客

时间轴

时间轴

2025-10-31

init

哈希表

题目:

用哈希表空间换时间

123456789101112131415161718192021222324
#include <vector>#include <unordered_map>using std::vector;using std::unordered_map;class Solution {    public:	vector<int> getSneakyNumbers(vector<int> &nums)	{		unordered_map<int, int> umap;		vector<int> res;		int i, n = nums.size();		for (i = 0; i < n; i++) {			umap[nums[i]]++;			if (umap[nums[i]] == 2) {				res.push_back(nums[i]);			}			if (res.size() == 2) {				break;			}		}		return res;	}};

这题的空间O(1)解法也挺有趣

评论加载中…