diff --git a/leetcode/1480.Running-Sum-of-1d-Array/1480.Running-Sum-of-1d-Array.go b/leetcode/1480.Running-Sum-of-1d-Array/1480.Running-Sum-of-1d-Array.go new file mode 100644 index 000000000..4a5f1a744 --- /dev/null +++ b/leetcode/1480.Running-Sum-of-1d-Array/1480.Running-Sum-of-1d-Array.go @@ -0,0 +1,19 @@ +package leetcode + +func runningSum(nums []int) []int { + result := []int{} + counter := 0 + + for x := 0; x < len(nums); x++ { + for y := 0; y < x; y++ { + counter += nums[y] + } + + val := counter + nums[x] + result = append(result, val) + + counter = 0 + } + + return result +} diff --git a/leetcode/1480.Running-Sum-of-1d-Array/1480.Running-Sum-of-1d-Array_test.go b/leetcode/1480.Running-Sum-of-1d-Array/1480.Running-Sum-of-1d-Array_test.go new file mode 100644 index 000000000..da95eea81 --- /dev/null +++ b/leetcode/1480.Running-Sum-of-1d-Array/1480.Running-Sum-of-1d-Array_test.go @@ -0,0 +1,52 @@ +package leetcode + +import ( + "fmt" + "testing" +) + +type question1480 struct { + para1480 + ans1480 +} + +// para 是参数 +// one 代表第一个参数 +type para1480 struct { + nums []int +} + +// ans 是答案 +// one 代表第一个答案 +type ans1480 struct { + one []int +} + +func Test_Problem1480(t *testing.T) { + + qs := []question1480{ + + { + para1480{[]int{1, 2, 3, 4}}, + ans1480{[]int{1, 2, 6, 10}}, + }, + + { + para1480{[]int{1, 1, 1, 1, 1}}, + ans1480{[]int{1, 2, 3, 4, 5}}, + }, + + { + para1480{[]int{3, 1, 2, 10, 1}}, + ans1480{[]int{3, 4, 6, 16, 17}}, + }, + } + + fmt.Printf("------------------------Leetcode Problem 1480------------------------\n") + + for _, q := range qs { + _, p := q.ans1480, q.para1480 + fmt.Printf("【input】:%v 【output】:%v \n", p, runningSum(p.nums)) + } + fmt.Printf("\n\n\n") +} diff --git a/leetcode/1480.Running-Sum-of-1d-Array/README.md b/leetcode/1480.Running-Sum-of-1d-Array/README.md new file mode 100644 index 000000000..3ed3e0ac5 --- /dev/null +++ b/leetcode/1480.Running-Sum-of-1d-Array/README.md @@ -0,0 +1,59 @@ +# [1480. Running Sum of 1d Array](https://leetcode.com/problems/running-sum-of-1d-array/) + +## 题目 + +Given an array `nums`. We define a running sum of an array as `runningSum[i] = sum(nums[0]…nums[i])`. + +Return the running sum of `nums`. + +**Example 1**: + +``` +Input: nums = [1,2,3,4] +Output: [1,3,6,10] +Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4]. + +``` + +**Example 2**: + +``` +Input: nums = [1,1,1,1,1] +Output: [1,2,3,4,5] +Explanation: Running sum is obtained as follows: [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1]. + +``` + +**Example 3**: + +``` +Input: nums = [3,1,2,10,1] +Output: [3,4,6,16,17] + +``` + +**Constraints**: + +- `1 <= nums.length <= 1000` +- `-10^6 <= nums[i] <= 10^6` + +```go +func runningSum(nums []int) []int { + result := []int{} + counter := 0 + + for x := 0; x < len(nums); x++ { + for y := 0; y < x; y++ { + counter += nums[y] + } + + val := counter + nums[x] + result = append(result, val) + + counter = 0 + } + + return result +} + +``` \ No newline at end of file diff --git a/leetcode/1512.Number-of-Good-Pairs/1512.Number-of-Good-Pairs.go b/leetcode/1512.Number-of-Good-Pairs/1512.Number-of-Good-Pairs.go new file mode 100644 index 000000000..a56f52d6c --- /dev/null +++ b/leetcode/1512.Number-of-Good-Pairs/1512.Number-of-Good-Pairs.go @@ -0,0 +1,14 @@ +package leetcode + +func numIdenticalPairs(nums []int) int { + total := 0 + for x := 0; x < len(nums); x++ { + for y := x + 1; y < len(nums); y++ { + if nums[x] == nums[y] { + total++ + } + } + } + + return total +} diff --git a/leetcode/1512.Number-of-Good-Pairs/1512.Number-of-Good-Pairs_test.go b/leetcode/1512.Number-of-Good-Pairs/1512.Number-of-Good-Pairs_test.go new file mode 100644 index 000000000..3fc122186 --- /dev/null +++ b/leetcode/1512.Number-of-Good-Pairs/1512.Number-of-Good-Pairs_test.go @@ -0,0 +1,52 @@ +package leetcode + +import ( + "fmt" + "testing" +) + +type question1512 struct { + para1512 + ans1512 +} + +// para 是参数 +// one 代表第一个参数 +type para1512 struct { + nums []int +} + +// ans 是答案 +// one 代表第一个答案 +type ans1512 struct { + one int +} + +func Test_Problem1512(t *testing.T) { + + qs := []question1512{ + + { + para1512{[]int{1, 2, 3, 1, 1, 3}}, + ans1512{4}, + }, + + { + para1512{[]int{1, 1, 1, 1}}, + ans1512{6}, + }, + + { + para1512{[]int{1, 2, 3}}, + ans1512{0}, + }, + } + + fmt.Printf("------------------------Leetcode Problem 1512------------------------\n") + + for _, q := range qs { + _, p := q.ans1512, q.para1512 + fmt.Printf("【input】:%v 【output】:%v \n", p, numIdenticalPairs(p.nums)) + } + fmt.Printf("\n\n\n") +} diff --git a/leetcode/1512.Number-of-Good-Pairs/README.md b/leetcode/1512.Number-of-Good-Pairs/README.md new file mode 100644 index 000000000..28a5fd0ca --- /dev/null +++ b/leetcode/1512.Number-of-Good-Pairs/README.md @@ -0,0 +1,57 @@ +# [1512. Number of Good Pairs](https://leetcode.com/problems/number-of-good-pairs/) + +## 题目 + +Given an array of integers `nums`. + +A pair `(i,j)` is called good if `nums[i] == nums[j]` and `i < j`. + +Return the number of good pairs. + +**Example 1**: + +``` +Input: nums = [1,2,3,1,1,3] +Output: 4 +Explanation: There are 4 good pairs (0,3), (0,4), (3,4), (2,5) 0-indexed. + +``` + +**Example 2**: + +``` +Input: nums = [1,1,1,1] +Output: 6 +Explanation: Each pair in the array are good. + +``` + +**Example 3**: + +``` +Input: nums = [1,2,3] +Output: 0 + +``` + +**Constraints**: + +- `1 <= nums.length <= 1000` +- `1 <= nums[i] <= 100` + +```go +func numIdenticalPairs(nums []int) int { + total := 0 + for x := 0; x < len(nums); x++ { + for y := x + 1; y < len(nums); y++ { + if nums[x] == nums[y] { + total++ + } + } + } + + return total +} + + +``` \ No newline at end of file