前缀和
- 题目链接
前缀和https://www.nowcoder.com/practice/acead2f4c28c401889915da98ecdc6bf?tpId=230&tqId=2021480&ru=/exam/oj&qru=/ta/dynamic-programming/question-ranking&sourceUrl=%2Fexam%2Foj%3Fpage%3D1%26tab%3D%25E7%25AE%2597%25E6%25B3%2595%25E7%25AF%2587%26topicId%3D196
- 算法原理
- 代码步骤
#include<iostream>
#include<vector>
using namespace std;int main()
{// 读入数据int n, q;cin >> n >> q;vector<int> arr(n + 1);for(int i = 1; i <= n; i++){cin >> arr[i];}// 预处理出来一个前缀和数组vector<long long> dp(n + 1);for(int i = 1; i <= n; i++){dp[i] = dp[i - 1] + arr[i];}// 使用前缀和数组while(q--){int l, r;cin >> l >> r;cout << dp[r] - dp[l - 1] << endl;}return 0;
}
二维前缀和
-
题目链接
二维前缀和https://www.nowcoder.com/practice/99eb8040d116414ea3296467ce81cbbc?tpId=230&tqId=2023819&ru=/exam/oj&qru=/ta/dynamic-programming/question-ranking&sourceUrl=%2Fexam%2Foj%3Fpage%3D1%26tab%3D%25E7%25AE%2597%25E6%25B3%2595%25E7%25AF%2587%26topicId%3D196
-
算法原理
-
代码步骤
#include <iostream>
#include <vector>using namespace std;int main()
{// 输入数据int n = 0, m = 0, q = 0;cin >> n >> m >> q;vector<vector<int>> arr(n + 1, vector<int>(m + 1));for(int i = 1; i <= n; i++){for(int j = 1; j <= m; j++){cin >> arr[i][j];}}// 预处理前缀和vector<vector<long long>> ap(n + 1, vector<long long>(m + 1));for(int i = 1; i <= n; i++){for(int j = 1; j <= m; j++){ap[i][j] = ap[i - 1][j] + ap[i][j - 1] + arr[i][j] - ap[i - 1][j - 1];}}// 使用前缀和int x1 = 0, y1 = 0, x2 = 0, y2 = 0;while(q--){cin >> x1 >> y1 >> x2 >> y2;cout << ap[x2][y2] - ap[x1 - 1][y2] - ap[x2][y1 - 1] + ap[x1 - 1][y1 - 1] << endl;}return 0;
}
寻找数组的中心下标
-
题目链接
寻找数组的中心下标https://leetcode.cn/problems/find-pivot-index/
-
算法原理
-
代码步骤
class Solution {
public:int pivotIndex(vector<int>& nums) {// 初始化数组int n = nums.size();vector<int> f(n), g(n);// 预处理前缀和和后缀和for(int i = 1; i <= n - 1; i++){f[i] = f[i - 1] + nums[i - 1];}for(int i = n - 2; i >= 0; i--){g[i] = g[i + 1] + nums[i + 1];}// 使用前缀和和后缀和for(int i = 0; i <= n - 1; i++){if(f[i]==g[i]){return i;}}return -1;}
};
除自身以外数组的乘积
-
题目链接
除自身以外数组的乘积https://leetcode.cn/problems/product-of-array-except-self/description/
-
算法原理
-
代码步骤
class Solution {
public:vector<int> productExceptSelf(vector<int>& nums) {int n = nums.size();vector<int> f(n), g(n);// 处理细节f[0] = g[n - 1] = 1;for(int i = 1; i <= n -1; i++){f[i] = f[i - 1] * nums[i - 1];}for(int i = n - 2; i >= 0; i--){g[i] = g[i + 1] * nums[i + 1];}// 使用vector<int> ret(n);for(int i = 0; i <= n - 1; i++){ret[i] = f[i] * g[i];}return ret;}
};
和为k的子数组
-
题目链接
和为k的子数组https://leetcode.cn/problems/subarray-sum-equals-k/description/
-
算法原理
-
代码步骤
class Solution {
public:int subarraySum(vector<int>& nums, int k) {int n = nums.size();unordered_map<int, int> hashi;hashi[0] = 1;int sum = 0, ret = 0;for(int i = 0; i <= n - 1; i++){sum += nums[i];ret += hashi[sum - k];hashi[sum]++;}return ret;}
};
和可被k整除的子数组
-
题目链接
和可被k整除的子数组https://leetcode.cn/problems/subarray-sums-divisible-by-k/description/
-
算法原理
-
代码步骤
class Solution {
public:int subarraysDivByK(vector<int>& nums, int k) {unordered_map<int,int> hashi;hashi[0 % k] = 1;int ret = 0, sum = 0;for(auto a : nums){sum += a;int r = (sum % k + k) % k;if(hashi.count(r)) {ret += hashi[r];}hashi[r]++;}return ret;}
};
连续数组
-
题目链接
连续数组https://leetcode.cn/problems/contiguous-array/
-
算法原理
-
代码步骤
class Solution {
public:int findMaxLength(vector<int>& nums) {unordered_map<int, int> hash;int len = 0, sum = 0;hash[0] = -1;for(int i = 0; i < nums.size(); i++){sum += nums[i] ? 1 : -1;if(hash.count(sum)){len = max(len, i - hash[sum]);}else{hash[sum] = i;}}return len;}
};
矩阵区域和
-
题目链接
矩阵区域和https://leetcode.cn/problems/matrix-block-sum/description/
-
算法原理
-
代码步骤
class Solution {
public:vector<vector<int>> matrixBlockSum(vector<vector<int>>& mat, int k) {int m = mat.size(), n = mat[0].size();vector<vector<int>> dp(m + 1, vector<int>(n + 1));for(int i = 1; i <= m; i++){for(int j = 1; j <= n; j++){dp[i][j] = dp[i][j - 1] + dp[i - 1][j] - dp[i - 1][j - 1] + mat[i - 1][j - 1];}}vector<vector<int>> ret(m, vector<int>(n));for(int i = 0; i < m; i++){for(int j = 0; j < n; j++){int x1 = max(0, i - k) + 1, y1 = max(0, j - k) + 1;int x2 = min(m - 1, i + k) + 1, y2 = min(n - 1, j + k) + 1;ret[i][j] = dp[x2][y2] - dp[x1 - 1][y2] - dp[x2][y1 - 1] + dp[x1 - 1][y1 - 1];}}return ret;}
};