# leetcode算法题：第一个唯一数字

``````class FirstUnique
{
public:
unordered_map<int,int> num_freq;
vector<int> rec;

FirstUnique(vector<int>& nums)
{
for (int x: nums)
num_freq[x] ++;
for (int x : nums)
if (num_freq[x] == 1)
rec.push_back(x);
}

int showFirstUnique()
{
int n = rec.size();
int res = -1;
int i = 0;
while (i < n)
{
if (num_freq[rec[i]] == 1)
{
res = rec[i];
rec.erase(rec.begin(), rec.begin() + i);
break;
}
i ++;
}
return res;

}

{
num_freq[value] ++;
if (num_freq[value] == 1)
{
rec.push_back(value);
}
}
};

/**
* Your FirstUnique object will be instantiated and called as such:
* FirstUnique* obj = new FirstUnique(nums);
* int param_1 = obj->showFirstUnique();
*/``````

while (i < n)
{
if (num_freq[rec[i]] == 1)
{
res = rec[i];
rec.erase(rec.begin(), rec.begin() + i);
break;
}
i ++;
}

• 写回答

#### 2条回答默认 最新

• 迷途的启木菌 2021-06-11 20:32
关注

已解决

本回答被题主选为最佳回答 , 对您是否有帮助呢?
评论

#### 悬赏问题

• ¥50 来个抓app跳转支付宝转链接的
• ¥15 remotes安装提示没有description文件
• ¥15 AttributeError: 'NoneType' object has no attribute 'drop_duplicates'报错
• ¥15 以下代码，运行结果报错