简单模拟题。
class Solution {
public:
vector<int> createTargetArray(vector<int>& nums, vector<int>& index) {
int len1 = nums.size();
vector<int> ans(len1);
for(int i = 0;i < len1; i++) {
for(int j = len1 - 1; j > index[i]; j--) {
ans[j] = ans[j - 1];
}
ans[index[i]] = nums[i];
}
return ans;
}
};
简单模拟题。
class Solution {
public:
bool arrayStringsAreEqual(vector<string>& word1, vector<string>& word2) {
int len1 = word1.size(), len2 = word2.size();
string ans1 = "", ans2 = "";
for(int i = 0;i < len1; i++)
ans1 += word1[i];
for(int i = 0;i < len2; i++)
ans2 += word2[i];
if(ans1 == ans2)
return true;
else
return false;
}
};
简单模拟题。
class Solution {
public:
int largestAltitude(vector<int>& gain) {
int max1 = 0, sum = 0,len1 = gain.size();
for(int i = 0;i < len1; i++) {
sum += gain[i];
max1 = max(max1, sum);
}
return max1;
}
};
简单模拟题。
class ParkingSystem {
public:
int big1 = 0, medium1 = 0, small1 = 0;
ParkingSystem(int big, int medium, int small) {
big1 = big;
medium1 = medium;
small1 = small;
}
bool addCar(int carType) {
bool flag = false;
if(carType == 1) {
if(big1 >= 1) {
big1 -= 1;
flag = true;
}
}
if(carType == 2) {
if(medium1 >= 1) {
medium1 -= 1;
flag = true;
}
}
if(carType == 3) {
if(small1 >= 1) {
small1 -= 1;
flag = true;
}
}
return flag;
}
};
/**
* Your ParkingSystem object will be instantiated and called as such:
* ParkingSystem* obj = new ParkingSystem(big, medium, small);
* bool param_1 = obj->addCar(carType);
*/
简单模拟题。
class Solution {
public:
vector<int> decode(vector<int>& encoded, int first) {
vector<int> ans;
int len1 = encoded.size();
ans.push_back(first);
for(int i = 0; i < len1; i++) {
int sum = ans[i] ^ encoded[i];
ans.push_back(sum);
}
return ans;
}
};
简单模拟题。
class Solution {
public:
string interpret(string command) {
string ans = "";
int len1 = command.length();
for(int i = 0;i < len1; i++) {
if(command[i] == 'G')
ans = ans + "G";
if(command[i] == '(') {
if(command[i+1] == ')') {
ans = ans + "o";
i += 1;
}
else {
ans = ans + "al";
i += 3;
}
}
}
return ans;
}
};
简单模拟题。
class Solution {
public:
vector<int> decompressRLElist(vector<int>& nums) {
vector<int> ans;
int len1 = nums.size();
for(int i = 0;i < len1; i+= 2) {
int sum = nums[i];
while(sum--)
ans.push_back(nums[i+1]);
}
return ans;
}
};
简单模拟题。
class Solution {
public:
int maxDepth(string s) {
int max1 = 0, sum = 0, len1 = s.length();
for(int i = 0; i < len1; i++) {
if(s[i] == '(')
sum += 1;
if(s[i] == ')')
sum -= 1;
max1 = max(max1, sum);
}
return max1;
}
};
简单模拟题。
class Solution {
public:
int numberOfSteps (int num) {
int step = 0;
while(num) {
if(num % 2 == 0)
num /= 2;
else
num -= 1;
step += 1;
}
return step;
}
};
简单模拟题。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
int getDecimalValue(ListNode* head) {
ListNode* head1 = head;
int ans = 0;
while (head1 != nullptr) {
ans = ans * 2 + head1->val;
head1 = head1->next;
}
return ans;
}
};
简单模拟题。
class Solution {
public:
int diagonalSum(vector<vector<int>>& mat) {
int len1 = mat.size(),len2 = mat[0].size(), sum = 0;
for (int i = 0; i < len1; i++) {
for (int j = 0; j < len2; j++) {
if (i == j || i + j == len1 - 1)
sum += mat[i][j];
}
}
return sum;
}
};
简单模拟题。
class Solution {
public:
int numberOfMatches(int n) {
int ans = 0;
while(n != 1) {
if(n % 2 == 0) {
n /= 2;
ans += n;
} else {
ans += (n/2 + 1);
n /= 2;
}
}
return ans;
}
};
简单模拟题。
class Solution {
public:
int busyStudent(vector<int>& startTime, vector<int>& endTime, int queryTime) {
int ans = 0;
int len1 = startTime.size();
for(int i = 0; i < len1; i++) {
if(startTime[i] <= queryTime && queryTime <= endTime[i])
ans += 1;
}
return ans;
}
};
简单模拟题。
class Solution {
public:
int calculate(string s) {
int x = 1, y = 0;
int len1 = s.length();
for(int i = 0; i < len1; i++) {
if(s[i] == 'A')
x = 2 * x + y;
else
y = 2 * y + x;
}
return x + y;
}
};
简单模拟题。
class Solution {
public:
bool halvesAreAlike(string s) {
int len1 = s.length();
int num1 = 0, num2 = 0;
for(int i = 0; i < len1 / 2; i++) {
if(s[i] == 'a')
num1 += 1;
if(s[i] == 'e')
num1 += 1;
if(s[i] == 'i')
num1 += 1;
if(s[i] == 'o')
num1 += 1;
if(s[i] == 'u')
num1 += 1;
if(s[i] == 'A')
num1 += 1;
if(s[i] == 'E')
num1 += 1;
if(s[i] == 'I')
num1 += 1;
if(s[i] == 'O')
num1 += 1;
if(s[i] == 'U')
num1 += 1;
}
for(int i = len1 / 2; i < len1; i++) {
if(s[i] == 'a')
num2 += 1;
if(s[i] == 'e')
num2 += 1;
if(s[i] == 'i')
num2 += 1;
if(s[i] == 'o')
num2 += 1;
if(s[i] == 'u')
num2 += 1;
if(s[i] == 'A')
num2 += 1;
if(s[i] == 'E')
num2 += 1;
if(s[i] == 'I')
num2 += 1;
if(s[i] == 'O')
num2 += 1;
if(s[i] == 'U')
num2 += 1;
}
return num1 == num2;
}
};
简单模拟题。
class Solution {
public:
string freqAlphabets(string s) {
string ans = "";
int len1 = s.length();
for(int i = 0;i < len1; i++) {
if(s[i] >= '1' && s[i] <= '9') {
if(i + 2 < len1 && s[i + 2] == '#') {
if(s[i] == '1' && s[i+1] == '0')
ans = ans + "j";
if(s[i] == '1' && s[i+1] == '1')
ans = ans + "k";
if(s[i] == '1' && s[i+1] == '2')
ans = ans + "l";
if(s[i] == '1' && s[i+1] == '3')
ans = ans + "m";
if(s[i] == '1' && s[i+1] == '4')
ans = ans + "n";
if(s[i] == '1' && s[i+1] == '5')
ans = ans + "o";
if(s[i] == '1' && s[i+1] == '6')
ans = ans + "p";
if(s[i] == '1' && s[i+1] == '7')
ans = ans + "q";
if(s[i] == '1' && s[i+1] == '8')
ans = ans + "r";
if(s[i] == '1' && s[i+1] == '9')
ans = ans + "s";
if(s[i] == '2' && s[i+1] == '0')
ans = ans + "t";
if(s[i] == '2' && s[i+1] == '1')
ans = ans + "u";
if(s[i] == '2' && s[i+1] == '2')
ans = ans + "v";
if(s[i] == '2' && s[i+1] == '3')
ans = ans + "w";
if(s[i] == '2' && s[i+1] == '4')
ans = ans + "x";
if(s[i] == '2' && s[i+1] == '5')
ans = ans + "y";
if(s[i] == '2' && s[i+1] == '6')
ans = ans + "z";
i += 2;
} else {
if(s[i] == '1')
ans = ans + "a";
if(s[i] == '2')
ans = ans + "b";
if(s[i] == '3')
ans = ans + "c";
if(s[i] == '4')
ans = ans + "d";
if(s[i] == '5')
ans = ans + "e";
if(s[i] == '6')
ans = ans + "f";
if(s[i] == '7')
ans = ans + "g";
if(s[i] == '8')
ans = ans + "h";
if(s[i] == '9')
ans = ans + "i";
}
}
}
return ans;
}
};
简单模拟题。
class Solution {
public:
int oddCells(int n, int m, vector<vector<int>>& indices) {
int num[55][55] = {0};
int len1 = indices.size();
for(int i = 0;i < len1; i++) {
int r1 = indices[i][0], c1 = indices[i][1];
for(int j = 0; j < m; j++)
num[r1][j] += 1;
for(int j = 0; j < n; j++)
num[j][c1] += 1;
}
int ans = 0;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
if(num[i][j] % 2)
ans += 1;
}
}
return ans;
}
};
简单模拟题。
class Solution {
public:
int countNegatives(vector<vector<int>>& grid) {
int num=0, len1 = grid.size(), len2 = grid[0].size();
for (int i = 0;i < len1; i++) {
for (int j = 0; j < len2; j++) {
if (grid[i][j] < 0)
num++;
}
}
return num;
}
};
简单模拟题。
class Solution {
public:
int maxProduct(vector<int>& nums) {
int len1 = nums.size();
sort(nums.begin(),nums.end());
return (nums[len1 - 1] - 1) * (nums[len1 - 2] - 1);
}
};
简单模拟题。
class Solution {
public:
vector<int> replaceElements(vector<int>& arr) {
int n = arr.size();
vector<int> ans(n);
ans[n - 1] = -1;
for (int i = n - 2; i >= 0; i--)
ans[i] = max(ans[i + 1], arr[i + 1]);
return ans;
}
};
简单模拟题。
class Solution {
public:
string restoreString(string s, vector<int>& indices) {
int length = s.length();
string result(length, 0);
for(int i = 0; i < length; i++) {
result[indices[i]] = s[i];
}
return result;
}
};
简单模拟题。
class Solution {
public:
int countGoodTriplets(vector<int>& arr, int a, int b, int c) {
int n = arr.size(), cnt = 0;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
for (int k = j + 1; k < n; ++k) {
if (abs(arr[i] - arr[j]) <= a && abs(arr[j] - arr[k]) <= b && abs(arr[i] - arr[k]) <= c) {
cnt += 1;
}
}
}
}
return cnt;
}
};
简单模拟题。
class Solution {
public:
int countGoodRectangles(vector<vector<int>>& rectangles) {
map<int,int> p;
int len1 = rectangles.size();
for(int i = 0; i < len1; i++) {
int temp = min(rectangles[i][0],rectangles[i][1]);
p[temp] += 1;
}
int max1 = -1;
for(map<int,int>::iterator it1 = p.begin();it1 != p.end(); it1++) {
if(it1->first > max1) {
max1 = it1 -> first;
}
}
return p[max1];
}
};
简单模拟题。
class Solution {
public:
string destCity(vector<vector<string>>& paths) {
set<string> p,q;
int len1 = paths.size();
for(int i = 0;i < len1;i++) {
p.insert(paths[i][0]);
q.insert(paths[i][1]);
}
string ans = "";
for(set<string>::iterator it1 = q.begin();it1 != q.end(); it1++) {
if(!p.count(*it1)) {
ans = *it1;
break;
}
}
return ans;
}
};
简单模拟题。其实就是看两个数组中的元素是否相同。
class Solution {
public:
bool canBeEqual(vector<int>& target, vector<int>& arr) {
int len1 = target.size(), len2 = arr.size();
if(len1 == len2) {
sort(target.begin(),target.end());
sort(arr.begin(),arr.end());
bool flag = true;
for(int i = 0; i < len1; i++) {
if(target[i] != arr[i]) {
flag = false;
break;
}
}
return flag;
} else
return false;
}
};
简单模拟题。规律题。分成奇数个数和偶数个数进行区分,如果奇数其中一个数为0,之后等同化为偶数的即可。
class Solution {
public:
vector<int> sumZero(int n) {
vector<int> ans;
int sum = 0;
for (int i = 0; i < n - 1; ++i) {
ans.push_back(i);
sum += i;
}
ans.push_back(-sum);
return ans;
}
};