Leetcode 209
#include "iostream"
#include "vector"
using namespace std;
int minSubArrayLen(int target, vector<int> &nums) {
int len = INT32_MAX;
int sum = 0;
for (int j = 0, i = 0; j < nums.size(); j++) {
sum += nums[j];
while (sum >= target) {
len = len > j - i ? j - i : len;
sum -= nums[i];
i++;
}
}
return len == INT32_MAX ? 0 : len + 1;
}
int main() {
vector<int> nums = {2, 3, 1, 2, 4, 3};
int target = 7;
int ans = minSubArrayLen(target, nums);
cout << ans << endl;
}
Leetcode 59
#include "iostream"
#include "vector"
#include "math.h"
using namespace std;
vector<vector<int>> generateMatrix(int n) {
vector<vector<int>> matrix(n, vector<int>(n));
int num = 1;
int i = 0, j = 0;
for (int circle = 1; circle <= ceil(n / 2.0); circle++) {
while (j <= n - circle) {
matrix[i][j++] = num++;
}
i++, j--;
while (i <= n - circle) {
matrix[i++][j] = num++;
}
i--, j--;
while (j >= circle - 1) {
matrix[i][j--] = num++;
}
i--, j++;
while (i > circle - 1) {
matrix[i--][j] = num++;
}
i++, j++;
}
return matrix;
}
int main() {
int n = 1;
vector<vector<int>> matrix = generateMatrix(n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
}
kama 44
#include "iostream"
#include "math.h"
using namespace std;
int main() {
int n, m;
cin >> n >> m;
int area[n + 1][m + 1];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> area[i][j];
}
}
int horizon = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
horizon += area[i][j];
}
area[i][m] = horizon;
}
int vertical = 0;
for (int j = 0; j < m; j++) {
for (int i = 0; i < n; i++) {
vertical += area[i][j];
}
area[n][j] = vertical;
}
int minn = INT32_MAX;
int sum = area[n][m - 1];
for (int i = 0; i < n; i++) {
minn = min(minn, abs(sum - area[i][m] * 2));
}
for (int j = 0; j < m; j++) {
minn = min(minn, abs(sum - area[n][j] * 2));
}
// for (int i = 0; i < n + 1; i++) {
// for (int j = 0; j < m + 1; j++) {
// cout << area[i][j] << " ";
// }
// cout << endl;
// }
cout << minn << endl;
}
kama 58
#include "iostream"
using namespace std;
int main() {
int n;
cin >> n;
int num[n];
int sum = 0;
for (int i = 0; i < n; i++) {
int a;
cin >> a;
sum += a;
num[i] = sum;
}
int a, b;
while (cin >> a >> b) {
if (a == 0) {
cout << num[b] << endl;
} else {
cout << num[b] - num[a - 1] << endl;
}
}
}
标签:kama,area,int,sum,随想录,++,vector,include,Leetcode
From: https://www.cnblogs.com/sz-maomao/p/18666983