832. Flipping an Image
class Solution {
public:
vector<vector<int>> flipAndInvertImage(vector<vector<int>>& A) {
vector<vector<int>> res =A;
for(int i=0;i<A.size();i++){
for(int j=A[0].size()-1, y=0;j>=0, y<A[0].size();j--, y++){
res[i][y] = !( A[i][j] ) ;
}
}
return res;
}
};
561. Array Partition I
class Solution {
public:
int arrayPairSum(vector<int>& nums) {
int res=0;
sort(nums.begin(),nums.end());
for(int i=0;i<nums.size();){
res+=nums[i];
i+=2;
}
return res;
}
};
766. Toeplitz Matrix
class Solution {
public:
bool isToeplitzMatrix(vector<vector<int>>& matrix) {
for(int i=0;i<matrix.size()-1;i++){
for(int j=0;j<matrix[0].size()-1;j++){
if(matrix[i][j]!=matrix[i+1][j+1]) return false;
}
}
return true;
}
};
566. Reshape the Matrix (beat100%)
class Solution {
public:
vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) {
int m=nums.size();
int n =nums[0].size();
if(m*n!=r*c) return nums;
vector<vector<int>> res(r, vector<int>(c));
for(int i=0;i<r*c;i++){
res[i/c][i%c]=nums[i/n][i%n];
}
return res;
}
};
485. Max Consecutive Ones
class Solution {
public:
int findMaxConsecutiveOnes(vector<int>& nums) {
int maxlen=0;
int cur =0;
for(int i=0;i<nums.size();i++){
if(nums[i]){
cur++;
maxlen=max(cur,maxlen);
}
else{
cur=0;
continue;
}
}
return maxlen;
}
};
Max Consecutive Ones Ⅱ
class Solution {
public:
int findMaxConsecutiveOnes(vector<int>& nums) {
int res = 0, left = 0, k = 1;
queue<int> q;
for (int right = 0; right < nums.size(); ++right) {
if (nums[right] == 0) q.push(right);
if (q.size() > k) {
left = q.front() + 1; q.pop();
}
res = max(res, right - left + 1);
}
return res;
}
};
695. Max Area of Island
695
.
Max Area of Island