publicclassSolution { publicintCountMatches(IList<IList<string>> items, string ruleKey, string ruleValue) { int index = new Dictionary<string, int>() { {"type", 0}, {"color", 1}, {"name", 2} }[ruleKey]; int res = 0; foreach (IList<string> item in items) { if (item[index].Equals(ruleValue)) { res++; } } return res; } }
[sol1-C++]
1 2 3 4 5 6 7 8 9 10 11 12 13
classSolution { public: intcountMatches(vector<vector<string>>& items, string ruleKey, string ruleValue){ unordered_map<string, int> dictionary = { {"type", 0}, {"color", 1}, {"name", 2} }; int res = 0, index = dictionary[ruleKey]; for (auto &&item : items) { if (item[index] == ruleValue) { res++; } } return res; } };
[sol1-C]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
intcountMatches(char *** items, int itemsSize, int* itemsColSize, char * ruleKey, char * ruleValue) { int res = 0, index = 0; if (strcmp(ruleKey, "type") == 0) { index = 0; } elseif (strcmp(ruleKey, "color") == 0) { index = 1; } elseif (strcmp(ruleKey, "name") == 0) { index = 2; } for (int i = 0; i < itemsSize; i++) { if (strcmp(items[i][index], ruleValue) == 0) { res++; } } return res; }
[sol1-JavaScript]
1 2 3 4 5 6 7 8 9 10
var countMatches = function(items, ruleKey, ruleValue) { const index = {"type":0, "color":1, "name":2}[ruleKey]; let res = 0; for (const item of items) { if (item[index] === ruleValue) { res++; } } return res; };
[sol1-Golang]
1 2 3 4 5 6 7 8 9 10 11
var d = map[string]int{"type": 0, "color": 1, "name": 2}
funccountMatches(items [][]string, ruleKey, ruleValue string) (ans int) { index := d[ruleKey] for _, item := range items { if item[index] == ruleValue { ans++ } } return }