[Preferred algorithm question practice] day9


1. DP35 [Template] Two-dimensional prefix sum

1. Introduction to the topic

DP35 [Template] Two-dimensional prefix sum.
Insert image description here
Insert image description here
Insert image description here
The question comes from Niuke.com and can be practiced through the link.

2. Problem-solving ideas

3.Code

#include <iostream>
using namespace std;
#include<vector>
int main() {
    
    
    int n, m, q;
    while (cin >> n >> m >> q) {
    
    
        vector<vector<long long>> nums(n + 1, vector<long long> (m + 1));
        for(int i = 1;i <= n; ++i)
        {
    
    
            for(int j = 1;j <= m; ++j)
            {
    
    
                cin>>nums[i][j];
            }
        }
        vector<vector<long long>> dp(n + 1, vector<long long> (m + 1));
        for(int i = 1;i <= n; ++i)
        {
    
    
            for(int j = 1;j <= m; ++j)
            {
    
    
                dp[i][j] = nums[i][j] + dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1];
            }
        }
        while(q--)
        {
    
    
            int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
            cin >> x1 >> y1 >> x2 >> y2;
            cout<<dp[x2][y2] - (dp[x1 - 1][y2] + dp[x2][y1 - 1] - dp[x1 - 1][y1 - 1])<<endl;
        }
    }
    return 0;
}

4. Running results

Insert image description here

2. Interview question 01.01. Determine whether characters are unique

1. Introduction to the topic

Interview question 01.01. Determine whether characters are unique.
Insert image description here
The question comes from Leetcode and can be practiced through the link.

2. Problem-solving ideas

3.Code

class Solution {
    
    
public:
    bool isUnique(string astr) {
    
    
        //用一个整数来充当哈希表
        int n = 0;
        for(auto& e : astr)
        {
    
    
            int t = e - 'a';
            if((n & (1 << t)) == 0)
            {
    
    
                n ^= (1 << t);
            }
            else
            {
    
    
                return false;
            }
        }
        return true;
    }
};

4. Running results

Insert image description here

3. 724. Find the center subscript of the array

1. Introduction to the topic

724. Find the center subscript of an array.
Insert image description here
Insert image description here
Insert image description here
The question comes from Leetcode and can be practiced through the link.

2. Problem-solving ideas

3.Code

class Solution {
    
    
public:
    int pivotIndex(vector<int>& nums) {
    
    
        vector<int> lsum(nums.size());//左边的前缀和
        vector<int> rsum(nums.size());//右边的前缀和
        for(int i = 1;i < nums.size(); ++i)
        {
    
    
            lsum[i] = lsum[i - 1] + nums[i - 1];
        }
        for(int i = nums.size() - 2;i >= 0; --i)
        {
    
    
            rsum[i] = rsum[i + 1] + nums[i + 1];
        }
        for(int i = 0;i < nums.size(); ++i)
        {
    
    
            if(lsum[i] == rsum[i]) return i;
        }
        return -1;
    }
};

4. Running results

Insert image description here


Summarize

Today is the 9th day of algorithm practice, keep up the good work.
If this article has inspired you, I hope you can support the author more, thank you all!

Guess you like

Origin blog.csdn.net/xjjxjy_2021/article/details/131949692