-
Notifications
You must be signed in to change notification settings - Fork 46
307 solved #71
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
Merged
307 solved #71
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
src/0307_Range_Sum_Query_Mutable/range_sum_query_mut.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
307. Range Sum Query - Mutable | ||
https://leetcode.com/problems/range-sum-query-mutable/ | ||
|
||
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. | ||
|
||
The update(i, val) function modifies nums by updating the element at index i to val. | ||
|
||
Note: | ||
The array is only modifiable by the update function. | ||
You may assume the number of calls to update and sumRange function is distributed evenly. | ||
*/ | ||
// time: 2019年01月29日 | ||
// 线段树时间复杂度为:O(log n) | ||
|
||
package rsqm | ||
|
||
// NumArray 线段树 | ||
type NumArray struct { | ||
segmentTree []int | ||
data []int | ||
} | ||
|
||
// Constructor 线段树生成函数 | ||
func Constructor(nums []int) NumArray { | ||
na := NumArray{segmentTree: make([]int, len(nums)*4), data: nums[:]} | ||
na.buildSegmentTree(0, 0, len(na.data)-1) | ||
return na | ||
} | ||
|
||
func (na *NumArray) buildSegmentTree(treeIndex, left, right int) { | ||
// 线段树处理的数据不能为空。 | ||
//if right < left { | ||
// return | ||
//} | ||
if left == right { | ||
na.segmentTree[treeIndex] = na.data[left] | ||
return | ||
} | ||
mid := left + (right-left)>>1 | ||
leftTreeIndex := 2*treeIndex + 1 | ||
rightTreeIndex := 2*treeIndex + 2 | ||
na.buildSegmentTree(leftTreeIndex, left, mid) | ||
na.buildSegmentTree(rightTreeIndex, mid+1, right) | ||
na.segmentTree[treeIndex] = na.segmentTree[leftTreeIndex] + na.segmentTree[rightTreeIndex] | ||
} | ||
|
||
// Update 线段树更新操作。 | ||
func (na *NumArray) Update(i int, val int) { | ||
na.setter(0, 0, len(na.data)-1, i, val) | ||
} | ||
|
||
func (na *NumArray) setter(treeIndex, left, right, index, val int) { | ||
if left == right { | ||
na.segmentTree[treeIndex] = val | ||
return | ||
} | ||
mid := left + (right-left)>>1 | ||
leftTreeIndex := 2*treeIndex + 1 | ||
rightTreeIndex := 2*treeIndex + 2 | ||
if index >= mid+1 { | ||
na.setter(rightTreeIndex, mid+1, right, index, val) | ||
} else { | ||
na.setter(leftTreeIndex, left, mid, index, val) | ||
} | ||
na.segmentTree[treeIndex] = na.segmentTree[leftTreeIndex] + na.segmentTree[rightTreeIndex] | ||
} | ||
|
||
// SumRange 线段树查询 | ||
func (na *NumArray) SumRange(i int, j int) int { | ||
return na.query(0, 0, len(na.data)-1, i, j) | ||
} | ||
|
||
func (na *NumArray) query(treeIndex, left, right, queryL, queryR int) int { | ||
if left == queryL && right == queryR { | ||
return na.segmentTree[treeIndex] | ||
} | ||
mid := left + (right-left)>>1 | ||
leftTreeIndex := 2*treeIndex + 1 | ||
rightTreeIndex := 2*treeIndex + 2 | ||
if queryL >= mid+1 { | ||
return na.query(rightTreeIndex, mid+1, right, queryL, queryR) | ||
} else if queryR <= mid { | ||
return na.query(leftTreeIndex, left, mid, queryL, queryR) | ||
} else { | ||
return na.query(leftTreeIndex, left, mid, queryL, mid) + na.query(rightTreeIndex, mid+1, right, mid+1, queryR) | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
src/0307_Range_Sum_Query_Mutable/range_sum_query_mut_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package rsqm | ||
|
||
import "testing" | ||
|
||
func TestNumArray(t *testing.T) { | ||
nums := []int{1, 2, 3, 6, 7, 8, 9, 3, 4, 2, 5} | ||
na := Constructor(nums) | ||
if res := na.SumRange(4, 9); res != 33 { | ||
t.Errorf("expected %d, got %d", 43, res) | ||
} | ||
na.Update(6, 10) | ||
if res := na.SumRange(4, 9); res != 34 { | ||
t.Errorf("expected %d, got %d", 44, res) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.