diff options
| author | crupest <crupest@outlook.com> | 2020-09-23 08:37:05 +0800 | 
|---|---|---|
| committer | crupest <crupest@outlook.com> | 2020-09-23 08:37:05 +0800 | 
| commit | 73b513453231758fc4273bea701212bd6fbb51d3 (patch) | |
| tree | c7131921153c43420fcbba0791536948f1d5fda6 /works/solutions/cpp | |
| parent | badc6cc5faca45d15728fa0dcc3cefc9e9d36f34 (diff) | |
| download | crupest-73b513453231758fc4273bea701212bd6fbb51d3.tar.gz crupest-73b513453231758fc4273bea701212bd6fbb51d3.tar.bz2 crupest-73b513453231758fc4273bea701212bd6fbb51d3.zip  | |
import(solutions): Add problem 303 .
Diffstat (limited to 'works/solutions/cpp')
| -rw-r--r-- | works/solutions/cpp/303.cpp | 26 | 
1 files changed, 26 insertions, 0 deletions
diff --git a/works/solutions/cpp/303.cpp b/works/solutions/cpp/303.cpp new file mode 100644 index 0000000..74aea3c --- /dev/null +++ b/works/solutions/cpp/303.cpp @@ -0,0 +1,26 @@ +#include <vector> + +using std::vector; + +class NumArray +{ +private: +    vector<int> prefix_sums; + +public: +    NumArray(vector<int> &nums) +    { +        prefix_sums.push_back(0); +        int sum = 0; +        for (auto num : nums) +        { +            sum += num; +            prefix_sums.push_back(sum); +        } +    } + +    int sumRange(int i, int j) +    { +        return prefix_sums[j + 1] - prefix_sums[i]; +    } +};  | 
