- 第一题
class Solution {
public:
string oddString(vector<string>& words) {
int size = words.size();
int len = words[0].length();
vector<int> vec1(len - 1, 0);
vector<int> vec2;
int ans = -1;
for(int i = 0; i < len - 1; i++){
vec1[i] = (int)(words[0][i + 1] - words[0][i]);
}
for(int j = 1; j < size; j++){
vector<int> temp(len - 1, 0);
for(int i = 0; i < len - 1; i++){
temp[i] = (int)(words[j][i + 1] - words[j][i]);
}
if(temp != vec1){
if(!vec2.empty()) return words[0];
else{
vec2 = temp;
ans = j;
}
}
}
return words[ans];
}
};
- 第二题
class Solution {
public:
vector<string> twoEditWords(vector<string>& queries, vector<string>& dictionary) {
int size = dictionary.size();
int size1 = queries.size();
int len = dictionary[0].length();
vector<string> ans;
for(int i = 0; i < size1; i++){
for(int j = 0; j < size; j++){
int time = 0;
for(int k = 0; k < len; k++){
if(queries[i][k] != dictionary[j][k]) time++;
}
if(time <= 2){
ans.push_back(queries[i]);
break;
}
}
}
return ans;
}
};
- 第三题
class Solution {
public:
int destroyTargets(vector<int>& nums, int space) {
// 统计每一组有多少个数
unordered_map<int, int> mp;
for (int x : nums) mp[x % space]++;
int ans = nums[0], best = mp[nums[0] % space];
for (int x : nums) {
int t = mp[x % space];
if (t > best || (t == best && x < ans)) ans = x, best = t;
}
return ans;
}
};
- 第四题
下面的解法TLE
class Solution {
public:
vector<int> secondGreaterElement(vector<int>& nums) {
int size = nums.size();
vector<int> ans(size, -1);
for(int i = 0; i < size; ++i){
int j = i + 1;
while(j < size && nums[j] <= nums[i]) j++;
if(j != size){
j++;
while(j < size && nums[j] <= nums[i]) j++;
if(j != size){
ans[i] = nums[j];
continue;
}
}
}
return ans;
}
};