File tree Expand file tree Collapse file tree 3 files changed +67
-0
lines changed
solution/0300-0399/0303.Range Sum Query - Immutable Expand file tree Collapse file tree 3 files changed +67
-0
lines changed Original file line number Diff line number Diff line change @@ -316,6 +316,30 @@ void numArrayFree(NumArray *obj) {
316316* /
317317```
318318
319+ ### **PHP**
320+
321+ ```php
322+ class NumArray {
323+ /**
324+ * @param Integer[] $nums
325+ */
326+ function __construct($nums) {
327+ $this->sum = [0];
328+ for ($i = 0; $i < count($nums); $i++) {
329+ array_push($this->sum, $this->sum[$i] + $nums[$i]);
330+ }
331+ }
332+ /**
333+ * @param Integer $left
334+ * @param Integer $right
335+ * @return Integer
336+ */
337+ function sumRange($left, $right) {
338+ return $this->sum[$right + 1] - $this->sum[$left];
339+ }
340+ }
341+ ```
342+
319343### ** ...**
320344
321345```
Original file line number Diff line number Diff line change @@ -300,6 +300,30 @@ void numArrayFree(NumArray *obj) {
300300* /
301301```
302302
303+ ### **PHP**
304+
305+ ```php
306+ class NumArray {
307+ /**
308+ * @param Integer[] $nums
309+ */
310+ function __construct($nums) {
311+ $this->sum = [0];
312+ for ($i = 0; $i < count($nums); $i++) {
313+ array_push($this->sum, $this->sum[$i] + $nums[$i]);
314+ }
315+ }
316+ /**
317+ * @param Integer $left
318+ * @param Integer $right
319+ * @return Integer
320+ */
321+ function sumRange($left, $right) {
322+ return $this->sum[$right + 1] - $this->sum[$left];
323+ }
324+ }
325+ ```
326+
303327### ** ...**
304328
305329```
Original file line number Diff line number Diff line change 1+ class NumArray {
2+ /**
3+ * @param Integer[] $nums
4+ */
5+ function __construct ($nums ) {
6+ $this -> sum = [0 ];
7+ for ($i = 0 ; $i < count ($nums ); $i ++ ) {
8+ array_push ($this -> sum , $this -> sum [$i ] + $nums [$i ]);
9+ }
10+ }
11+ /**
12+ * @param Integer $left
13+ * @param Integer $right
14+ * @return Integer
15+ */
16+ function sumRange ($left , $right ) {
17+ return $this -> sum [$right + 1 ] - $this -> sum [$left ];
18+ }
19+ }
You can’t perform that action at this time.
0 commit comments