简单模拟题。就一个个判定即可。
class Solution {
public:
string maximumTime(string time) {
if(time[0] == '?') {
if(time[1] == '?')
time[0] = '2', time[1] = '3';
else {
if(time[1] >= '0' && time[1] <= '3')
time[0] = '2';
else
time[0] = '1';
}
} else {
if(time[0] == '0') {
if(time[1] == '?')
time[1] = '9';
}
if(time[0] == '1') {
if(time[1] == '?')
time[1] = '9';
}
if(time[0] == '2') {
if(time[1] == '?')
time[1] = '3';
}
}
if(time[3] == '?') {
if(time[4] == '?')
time[3] = '5', time[4] = '9';
else
time[3] = '5';
} else {
if(time[4] == '?')
time[4] = '9';
}
return time;
}
};
简单模拟题。使用哈希表解决。
class Solution {
public:
bool checkIfExist(vector<int>& arr) {
int cnt[4001] = {0};
int* hash_set = cnt + 2000;
for (int n : arr)
++hash_set[n];
for (int n : arr)
if (n != 0 && hash_set[2 * n] >= 1)
return true;
else if (n == 0 && hash_set[2 * n] >= 2)
return true;
return false;
}
};
简单模拟题。判定前后是否为问号,同时保证前后字符与其不相同即可。
class Solution {
public:
string modifyString(string s) {
int len1 = s.length();
if(len1 == 1)
s = "a";
else {
for(int i = 0; i < len1 ; i++) {
if(s[i] == '?') {
if(i == 0) {
if(s[i + 1] == '?')
s[i] = 'a';
else {
if(s[i + 1] == 'z')
s[i] = 'y';
else
s[i] = s[i + 1] + 1;
}
} else {
if(i == len1 - 1) {
if(s[i - 1] == 'z')
s[i] = 'y';
else
s[i] = s[i - 1] + 1;
} else {
if(s[i+1] == '?') {
if(s[i - 1] == 'z')
s[i] = 'y';
else
s[i] = s[i - 1] + 1;
} else {
for(char j = 'a'; j <= 'z'; j++) {
if(s[i-1] != j && s[i + 1] != j) {
s[i] = j;
break;
}
}
}
}
}
}
}
}
return s;
}
};
简单模拟题。以@为分隔符,之前的所有字符判定.和+,之后的则全部记录,然后去重保存不同种类的个数即可。
class Solution {
public:
int numUniqueEmails(vector<string>& emails) {
set<string> p;
int len1 = emails.size();
for(int i = 0; i < len1; i++) {
int len2 = emails[i].length();
string tempz = "";
for(int j = 0; j < len2 ; j++) {
if(emails[i][j] == '.')
continue;
if(emails[i][j] == '+') {
while(j < len2 && emails[i][j]!='@')
j++;
while(j < len2) {
tempz = tempz + emails[i][j];
j += 1;
}
p.insert(tempz);
break;
}
if(emails[i][j] == '@') {
while(j < len2) {
tempz = tempz + emails[i][j];
j += 1;
}
p.insert(tempz);
break;
}
tempz += emails[i][j];
}
}
return p.size();
}
};
简单模拟题。从1开始模拟输入,遍历数组,判定和当前需要放进的数字是否相同,如果不相同,说明有push和pop操作,否则就是单纯的push操作。
class Solution {
public:
vector<string> buildArray(vector<int>& target, int n) {
vector<string> ans;
int len1 = target.size(), sum = 1;
for(int i = 0;i < len1; ) {
if(target[i] == sum) {
ans.push_back("Push");
sum += 1;
i += 1;
} else {
ans.push_back("Push");
ans.push_back("Pop");
sum += 1;
}
}
return ans;
}
};
简单模拟题。先记录数字个数,之后每三个存储,过程中记得判定当前还剩下几个字符,当剩下2,3,4时直接操作即可。
class Solution {
public:
string reformatNumber(string number) {
int len1 = number.length();
string temp = "";
for(int i = 0;i < len1; i++) {
if(number[i] >= '0' && number[i] <= '9')
temp += number[i];
}
string ans = "";
int len2 = temp.length();
if(len2 == 2 || len2 == 3)
ans = temp;
else {
if(len2 == 4)
ans = ans + temp[0] + temp[1] + "-" + temp[2] + temp[3];
else {
for(int i = 0; i < len2; i+=3) {
if((len2 - i) <= 4) {
if((len2 - i) == 2)
ans = ans + temp[i] + temp[i + 1];
if((len2 - i) == 3)
ans = ans + temp[i] + temp[i + 1] + temp[i + 2];
if((len2 - i) == 4)
ans = ans + temp[i] + temp[i + 1] + "-" + temp[i + 2] + temp[i + 3];
break;
} else
ans = ans + temp[i] + temp[i+1] + temp[i+2] + "-";
}
}
}
return ans;
}
};
简单模拟题。直接遍历排序后的数组,记录根据下标获得当前大于当前数字的个数,然后判定特征值是否存在。
class Solution {
public:
int specialArray(vector<int>& nums) {
sort(nums.begin(),nums.end());
int len1 = nums.size(), ans = -1;
for(int i = 0; i < len1; i++) {
int sum = len1 - i;
if(i == 0) {
if(sum <= nums[i]) {
ans = sum;
break;
}
} else {
if(sum <= nums[i] && sum > nums[i-1]) {
ans = sum;
break;
}
}
}
return ans;
}
};
简单模拟题。哈希表,存放查找。
class Solution {
public:
int findLucky(vector<int>& arr) {
map<int, int> p;
int len1 = arr.size();
for(int i = 0; i < len1; i++)
p[arr[i]] += 1;
int max1 = -1;
for(map<int, int>::iterator it1 = p.begin(); it1 != p.end(); it1++) {
if(it1 -> second == it1 -> first)
max1 = max(max1, it1->first);
}
return max1;
}
};
简单模拟题。哈希表记录存放数字的和,之后对哈希表排序即可返回最终的结果。
class Solution {
public:
int countLargestGroup(int n) {
unordered_map<int, int> hashMap;
int maxValue = 0;
for (int i = 1; i <= n; ++i) {
int key = 0, i0 = i;
while (i0) {
key += i0 % 10;
i0 /= 10;
}
++hashMap[key];
maxValue = max(maxValue, hashMap[key]);
}
int count = 0;
for (auto& kvpair: hashMap) {
if (kvpair.second == maxValue) {
++count;
}
}
return count;
}
};
简单模拟题。存放两个变量,之后重写排序规则,即可得出答案。
class Solution {
public:
vector<int> kWeakestRows(vector<vector<int>>& mat, int k) {
int n = mat.size();
vector<pair<int, int>> power;
for (int i = 0; i < n; ++i) {
int sum = accumulate(mat[i].begin(), mat[i].end(), 0);
power.emplace_back(sum, i);
}
sort(power.begin(), power.end());
vector<int> ans;
for (int i = 0; i < k; ++i) {
ans.push_back(power[i].second);
}
return ans;
}
};