250:题意很好理解,就是对每个字符串的一个最小前缀且满足任意俩个字符串的前缀都不相同
#include <iostream> #include <cstring> #include <string> #include <vector> #include <cstdio> using namespace std; class WordAbbreviation { public: vector <string> getAbbreviations(vector <string> words) { vector <string> res; res.clear(); for (int i = 0; i < words.size(); i++) { for (int j = 1; j <= words[i].size(); j++) { string str = words[i].substr(0, j); bool flag = false; for (int k = 0; k < words.size(); k++) { if (k == i) continue; if (str == words[k].substr(0, j)) flag = true; } if (!flag) { res.push_back(s); break; } } } return res; } };
500: 也是一道水题,题意是。。。额表达不好,就不说了
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <vector> using namespace std; class ConsecutiveNumbers { public: vector <int> missingNumber(vector <int> numbers) { vector<int> res; int len = numbers.size(); int tmp = 0, tem = 0; sort(numbers.begin(), numbers.end()); for (int i = 0; i < len-1; i++) { if (numbers[i] == numbers[i+1] || numbers[i+1] - numbers[i] > 2) return res; if (numbers[i+1] - numbers[i] == 2) { tem = numbers[i] + 1; tmp++; } } if (tmp == 1) { res.push_back(tem); return res; } else if (tmp >= 2) { return res; } else if (!tmp) { if (numbers[0] - 1 >= 1) res.push_back(numbers[0]-1); res.push_back(numbers[len-1]+1); } } };1000:是求期望?