Cover image for LeetCode Daily Problem P3289: The Troublemaker in Digital Town

LeetCode Daily Problem P3289: The Troublemaker in Digital Town


Timeline

Timeline

2025-10-31

init

Hash table

Problem:

Use a hash table to trade space for time

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;	}};

The O(1) space solution for this problem is also interesting

Loading comments…