力扣第321场周赛
力扣https://leetcode.cn/problems/find-the-pivot-integer/
枚举所有数并检查是否为中枢数,是则返回当前枚举数
class Solution {
public:
int pivotInteger(int n) {
for (int i = 1; i <= n; i++) {
int L = (1 + i) * i / 2;
int R = (i + n) * (n - i + 1) / 2;
if (L == R) return i;
}
return -1;
}
};
2.追加字符以获得子序列
力扣https://leetcode.cn/problems/append-characters-to-string-to-make-subsequence/找 s 里有多少个与 t 中相同的,补上 t 的长度减去相同字符个数即可
class Solution {
public:
int appendCharacters(string s, string t) {
int l = 0;
for(int i = 0;i < s.size();i++)
{
if(s[i] == t[l])
l++;
}
return t.size() - l;
}
};
力扣第92场双周赛
力扣https://leetcode.cn/problems/minimum-cuts-to-divide-a-circle/n=1时,不用分割,输出0,n为奇数输出n,n为偶数输出n/2
class Solution {
public:
int numberOfCuts(int n) {
if (n == 1) return 0;
else if (n & 1) return n;
else return n / 2;
}
};
2.行和列中一和零的差值
力扣https://leetcode.cn/problems/difference-between-ones-and-zeros-in-row-and-column/遍历grid,求出每一行0,1的个数和每一列0,1的个数,最后在grid上进行更新并返回
class Solution {
public:
vector<vector<int>> onesMinusZeros(vector<vector<int>>& grid) {
int m = grid.size();
int n = grid[0].size();
vector<int> hang0(m);
vector<int> lie0(n);
vector<int> hang1(m);
vector<int> lie1(n);
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
if(grid[i][j] == 0){
hang0[i]++;
lie0[j]++;
}else{
hang1[i]++;
lie1[j]++;
}
}
}
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
grid[i][j] = hang1[i] + lie1[j] - hang0[i] - lie0[j];
}
}
return grid;
}
};