Leetcode Note: Go - Range Sum Query Immutable

Range Sum Query - Immutable - LeetCode
https://leetcode.com/problems/range-sum-query-immutable/

  • Go 言語で取り組んだメモ

所感

  • int 型の nums 配列
  • NumArray
    • Constructor で nums を受け取る
    • SumRange
      • int 型の引数 left, right を受け取り、 nums におけるその範囲の合計を返す

回答

golang proper solution - LeetCode Discuss
https://leetcode.com/problems/range-sum-query-immutable/discuss/1454097/golang-proper-solution

type NumArray struct {
	Nums []int
}

func Constructor(nums []int) NumArray {
	x := NumArray{
		Nums: nums,
	}
	return x
}

func (this *NumArray) SumRange(left int, right int) int {
	sum := 0
	for i := left; i <= right; i++ {
		sum += this.Nums[i]
	}
	return sum
}

/**
 * Your NumArray object will be instantiated and called as such:
 * obj := Constructor(nums);
 * param_1 := obj.SumRange(left,right);
 */