#include <iostream>
#include <vector>
#include <unordered_set>
#include <unordered_map>
using namespace std;
string getCards(vector<int>& cards) {
unordered_map<int, bool> umap;
vector<int> result;
for (int i = 0; i < cards.size(); i++) {
if(umap.find(cards[i]) == umap.end()) {
result.push_back(cards[i]);
umap[cards[i]] = false;
}
else {
if(umap[cards[i]] == false) {
umap[cards[i]] = true;
}
else {
result.push_back(cards[i]);
}
}
}
string str = "[";
for(int i = 0; i < result.size(); i++) {
str += to_string(result[i]);
str += ",";
}
str[str.size() - 1] = ']';
return str;
}
int main() {
// vector<int> cards = {0, 1, 2, 2, 2, 3, 4, 4, 4};
vector<int> cards = {0, 2, 1, 2, 3, 10, 2, 2, 3, 4, 10, 4, 4};
string result = getCards(cards);
cout << result << endl;
return 0;
}
标题:
标题:卡片分配 | 时间限制:1秒 | 内存限制:262144K 编辑
限定语言:不限
老张和女儿一起玩芭比娃娃卡片游戏,老张希望女儿可以获得更多的卡片,老张只从重复的卡片中抽取一张留给自己,请帮助老张设计程序,输出女儿得到卡片列表
设:老张的卡片使用数组cards存储,不同的数字代表不同的芭比娃娃卡片。
示例1
输入
[0,1,2,2,2,3,4,4,4]
输出
"[0, 1, 2, 2, 3, 4, 4]"
说明
卡片种类:[0,1,2,2,3,3] ,不同种类的卡片包含 [ 0,1,2,3 ] ,所以女儿可以获得4种卡片,请注意返回值为String
示例2
输入
[1, 2]
输出
"[1, 2]"
说明
共有卡片[1,2] ,其中没有重复的,所以女儿获得全部卡片;请注意返回值为String
| 时间限制:1秒 | 内存限制:262144K 编辑
限定语言:不限
老张和女儿一起玩芭比娃娃卡片游戏,老张希望女儿可以获得更多的卡片,老张只从重复的卡片中抽取一张留给自己,请帮助老张设计程序,输出女儿得到卡片列表
设:老张的卡片使用数组cards存储,不同的数字代表不同的芭比娃娃卡片。
示例1
输入
[0,1,2,2,2,3,4,4,4]
输出
"[0, 1, 2, 2, 3, 4, 4]"
说明
卡片种类:[0,1,2,2,3,3] ,不同种类的卡片包含 [ 0,1,2,3 ] ,所以女儿可以获得4种卡片,请注意返回值为String
示例2
输入
[1, 2]
输出
"[1, 2]"
说明
共有卡片[1,2] ,其中没有重复的,所以女儿获得全部卡片;请注意返回值为String