classSolution{public:intfindMiddleIndex(vector<int>& nums){int n = nums.size();
vector<int>f(n +2);for(int i =1; i <= n; i++) f[i]= f[i -1]+ nums[i -1];for(int i =1; i <= n; i ++)if(f[i -1]== f[n]- f[i])return i -1;return-1;}};
LeetCode 1992. 找到所有的农场组
classSolution{public:
vector<vector<int>>findFarmland(vector<vector<int>>& g){int n = g.size(), m = g[0].size();
vector<vector<int>> res;for(int i =0; i < n; i ++){for(int j =0; j < m; j ++){if(g[i][j]){int x = i, y = j;for(int a = i; a < n && g[a][j]; a ++)for(int b = j; b < m && g[a][b]; b ++){
g[a][b]=0;
x =max(a, x), y =max(y, b);}
res.push_back({i, j, x, y});}}}return res;}};
LeetCode 1993. 树上的操作
classLockingTree{public:
vector<int> p;
vector<vector<int>> g;
vector<int> user;int n;LockingTree(vector<int>& parent){
p = parent;int n = p.size();
user.resize(n,-1);
g.resize(n);for(int i =1; i < n; i ++)
g[p[i]].push_back(i);}boollock(int k,int u){if(user[k]!=-1)returnfalse;
user[k]= u;returntrue;}boolunlock(int k,int u){if(user[k]!= u)returnfalse;
user[k]=-1;returntrue;}intdfs(int u){int res =0;if(user[u]!=-1){
user[u]=-1;
res ++;}for(auto son : g[u])
res +=dfs(son);return res;}boolupgrade(int k,int u){if(user[k]!=-1)returnfalse;for(int i = p[k]; i !=-1; i = p[i])if(user[i]!=-1)returnfalse;int cnt =dfs(k);if(cnt){
user[k]= u;returntrue;}returnfalse;}};/**
* Your LockingTree object will be instantiated and called as such:
* LockingTree* obj = new LockingTree(parent);
* bool param_1 = obj->lock(num,user);
* bool param_2 = obj->unlock(num,user);
* bool param_3 = obj->upgrade(num,user);
*/
Leetcode1994. 好子集的数目
typedeflonglong LL;constint MOD =1e9+7, N =31;classSolution{public:int g[N][N]={0};int s[N]={0};int st[N]={0};
vector<int> path;int C =1;intgcd(int a,int b){return b ?gcd(b, a % b): a;}intdfs(int u,int sum){if(!sum)return0;if(u >30){if(path.empty())return0;return sum *(LL)C % MOD;}int res =dfs(u +1, sum);if(!st[u]){bool flag =true;for(auto x : path)if(g[u][x]){
flag =false;break;}if(flag){
path.push_back(u);
res =(res +dfs(u +1, sum *(LL)s[u]% MOD))% MOD;
path.pop_back();}}return res;}intnumberOfGoodSubsets(vector<int>& nums){for(auto& t : nums) s[t]++;for(int i =0; i < s[1]; i ++)
C = C *2% MOD;for(int i =2; i * i <=30; i ++)for(int j =1; j * i * i <=30; j ++)
st[j * i * i]=1;for(int i =1; i <=30; i ++)for(int j =1; j <=30; j ++)if(gcd(i, j)>1)
g[i][j]=1;returndfs(2,1);}};
LeetCode 1991. 找到数组的中间位置class Solution {public: int findMiddleIndex(vector<int>& nums) { int n = nums.size(); vector<int> f(n + 2); for (int i = 1; i <= n; i++ ) f[i] = f[i - 1] + nums[i - 1]; for (i