It's not as easy to edit here, so I might as well paste a link
https://www.notion.so/Ways-of-compiling-11cb727340be80e4aa1dd61467db5b19?pvs=4
# | User | Rating |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3839 |
3 | Radewoosh | 3646 |
4 | jqdai0815 | 3620 |
4 | Benq | 3620 |
6 | orzdevinwang | 3612 |
7 | Geothermal | 3569 |
8 | ecnerwala | 3494 |
9 | Um_nik | 3396 |
10 | gamegame | 3386 |
# | User | Contrib. |
---|---|---|
1 | maomao90 | 164 |
2 | Um_nik | 163 |
3 | atcoder_official | 158 |
3 | cry | 158 |
3 | -is-this-fft- | 158 |
6 | awoo | 155 |
7 | adamant | 154 |
7 | nor | 154 |
9 | TheScrasse | 153 |
10 | Dominater069 | 152 |
It's not as easy to edit here, so I might as well paste a link
https://www.notion.so/Ways-of-compiling-11cb727340be80e4aa1dd61467db5b19?pvs=4
The problem appears to be:
==> Downloading and installing Homebrew... fatal: unable to access 'https://github.com/Homebrew/brew/': Error in the HTTP2 framing layer Failed during: /usr/bin/git fetch --quiet --progress --force origin
How the problem is solved:
the difference between using a reference (&) and a regular member variable
Let me explain the difference between using a reference (&) and a regular member variable in this context, and why a regular member variable is preferred.
References in C++ In C++, a reference is an alias for another variable. When you declare a reference, it must be initialized to refer to an existing object, and it cannot be reassigned to refer to another object after initialization.
Using a Reference When you declare a member variable as a reference, you are essentially saying that this member variable will always refer to some other existing variable. Here is what it looks like in your context:
cpp Copy code class NumMatrix { public: vector<vector>& pre_sum;
NumMatrix(vector<vector<int>>& matrix) : pre_sum(matrix) { // Implementation }
}; Here, pre_sum is a reference to matrix. This means pre_sum will always refer to matrix and cannot be reassigned to refer to something else. This is not suitable for your use case because you want pre_sum to be an independent data structure that holds the prefix sums, not just a reference to the input matrix.
Using a Regular Member Variable A regular member variable is an independent variable that is part of the object and can be initialized and modified as needed. Here is what it looks like in your context:
cpp Copy code class NumMatrix { public: vector<vector> pre_sum;
NumMatrix(vector<vector<int>>& matrix) { // Check if the input matrix is empty if (matrix.empty() || matrix[0].empty()) return; int m = matrix.size(); int n = matrix[0].size(); // Initialize pre_sum with the same dimensions as matrix pre_sum = vector<vector<int>>(m, vector<int>(n, 0)); // Calculate prefix sums pre_sum[0][0] = matrix[0][0]; for(int i = 1; i < m; ++i) { pre_sum[i][0] = pre_sum[i-1][0] + matrix[i][0]; } for(int j = 1; j < n; ++j) { pre_sum[0][j] = pre_sum[0][j-1] + matrix[0][j]; } for(int i = 1; i < m; ++i) { for(int j = 1; j < n; ++j) { pre_sum[i][j] = pre_sum[i][j-1] + pre_sum[i-1][j] - pre_sum[i-1][j-1] + matrix[i][j]; } } // Debug output for(int i = 0; i < m; ++i) { for(int j = 0; j < n; ++j) { cout << pre_sum[i][j] << ", "; } cout << endl; } } int sumRegion(int row1, int col1, int row2, int col2) { if (row1 == 0 && col1 == 0) return pre_sum[row2][col2]; else if (row1 == 0) return pre_sum[row2][col2] - pre_sum[row2][col1-1]; else if (col1 == 0) return pre_sum[row2][col2] - pre_sum[row1-1][col2]; return pre_sum[row2][col2] - pre_sum[row1-1][col2] - pre_sum[row2][col1-1] + pre_sum[row1-1][col1-1]; }
}; Here, pre_sum is an independent vector<vector> that is part of the NumMatrix object. It is initialized in the constructor and then filled with the prefix sums. This way, pre_sum is completely separate from the input matrix and can be modified independently.
Why a Regular Member Variable is Preferred Independence: pre_sum should be an internal data structure that stores the prefix sums independently of the input matrix. Flexibility: Using a regular member variable allows you to reinitialize or modify pre_sum as needed, whereas a reference must always refer to the same object it was initialized with. Encapsulation: It helps in encapsulating the logic and data within the NumMatrix class, which is a good practice in object-oriented programming. Summary In your case, pre_sum needs to be an independent data structure that the NumMatrix class manages internally. Using a regular member variable for pre_sum achieves this, while using a reference would incorrectly tie pre_sum to the input matrix, limiting its functionality and independence.
Name |
---|