-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
采用线段树进行编写update(int index, int newVal)方法
- Loading branch information
Showing
4 changed files
with
63 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
40 changes: 40 additions & 0 deletions
40
src/main/java/com/ximo/datastructuresinaction/leetcode/solution307/NumArray2.java
This file contains 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,40 @@ | ||
package com.ximo.datastructuresinaction.leetcode.solution307; | ||
|
||
import com.ximo.datastructuresinaction.segmenttree.SegmentTree; | ||
|
||
import java.util.Arrays; | ||
|
||
/** | ||
* @author xikl | ||
* @date 2019/1/19 | ||
*/ | ||
public class NumArray2 { | ||
|
||
private SegmentTree<Integer> segmentTree; | ||
|
||
|
||
public NumArray2(int[] nums) { | ||
Integer[] newNums = Arrays.stream(nums).boxed().toArray(Integer[]::new); | ||
|
||
segmentTree = new SegmentTree<>(newNums, (prev, next) -> prev + next); | ||
|
||
} | ||
|
||
public int sumRange(int i, int j) { | ||
return segmentTree.queryRangeClose(i, j); | ||
} | ||
|
||
|
||
/** | ||
* 更新其中的一个值 | ||
* | ||
* @param i 需要被更新的位置 | ||
* @param val 新的值 | ||
*/ | ||
public void update(int i, int val) { | ||
segmentTree.set(i, val); | ||
} | ||
|
||
|
||
|
||
} |
This file contains 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