Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

303. Range Sum Query - Immutable #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
zwfang merged 1 commit into master from range-sum-query-immutable
Dec 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ continually updating 😃.
* [215. Kth Largest Element in an Array](./src/0215_kth_largest_element_in_an_array/kthleiaa.go)   *`sort`*
* [283. Move Zeroes(solution1)](./src/0283_move_zeroes/move_zeroes.go)   *`sliding window`*
* [283. Move Zeroes(solution2)](./src/0283_move_zeroes/move_zeroes2.go)   *`sliding window`*
* [303. Range Sum Query - Immutable](src/0303_range_sum_query/rsqim.go)
* [349. Intersection of Two Arrays](./src/0349_intersection_of_2_arrays/intersection_of_two_arrays.go)   *`set`*
* [350. Intersection of Two Arrays II](./src/0350_intersection_of_two_arrays2/intersection_of_two_arrays2.go)   *`hash table`*
* [447. Number of Boomerangs](./src/0447_number_of_boomerangs/number_of_boomerangs.go)   *`hash table`*
Expand Down
30 changes: 30 additions & 0 deletions src/0303_range_sum_query/rsqim.go
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
303. Range Sum Query - Immutable
https://leetcode.com/problems/range-sum-query-immutable/

Given an integer array nums,
find the sum of the elements between indices i and j (i ≤ j), inclusive.
*/
// time: 2018年12月28日

package rsqim

// NumArray store sum of nums sub-list.
type NumArray struct {
sum []int
}

// Constructor make NumArray instance.
func Constructor(nums []int) NumArray {
nA := NumArray{sum: make([]int, 1)}
for _, num := range nums {
nA.sum = append(nA.sum, nA.sum[len(nA.sum)-1]+num)
}
return nA
}

// SumRange find the sum of the elements between indices i and j(i <= j), inclusive.
func (na *NumArray) SumRange(i int, j int) int {
j++
return na.sum[j] - na.sum[i]
}
22 changes: 22 additions & 0 deletions src/0303_range_sum_query/rsqim_test.go
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package rsqim

import "testing"

func TestSumRange(t *testing.T) {
nums := []int{-2, 0, 3, -5, 2, -1}
ranges := [][]int{
{0, 2},
{2, 5},
{0, 5},
}

expected := []int{1, -1, -3}

obj := Constructor(nums)

for index, section := range ranges {
if res := obj.SumRange(section[0], section[1]); res != expected[index] {
t.Errorf("expected %d, got %d", expected[index], res)
}
}
}
1 change: 1 addition & 0 deletions src/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
|0226|[Invert Binary Tree](./0226_invert_binary_tree/invert_binary_tree.go)|Easy|*`recursion; `* *`binary tree`*|
|0283|[Move Zeroes(solution1)](./0283_move_zeroes/move_zeroes.go) <br/> [Move Zeroes(solution2)](./0283_move_zeroes/move_zeroes2.go)|Easy|*`array`*|
|0300|[Longest Increasing Subsequence](./0300_longest_increasing_subsequence/lis.go)|Medium|*`dp`*|
|0303|[303. Range Sum Query - Immutable](0303_range_sum_query/rsqim.go)|Easy||
|0343|[Integer Break](./0343_integer_break/integer_break.go)|Medium|*`recursion;`* *`memory search;`* *`dynamic programming`*|
|0344|[344. Reverse String](0344_reverse_string/reverse_string.go)|Easy|*`double index`*|
|0345|[345. Reverse Vowels of a String](0345_reverse_vowels_of_a_string/reverse_vowels.go)|Easy|*`double index`*|
Expand Down

AltStyle によって変換されたページ (->オリジナル) /