Skip to content

Commit 44f8b5b

Browse files
Harry LeonardoHarry Leonardo
authored andcommitted
[ADD] 1480 & 1512
1 parent 5830c8d commit 44f8b5b

File tree

6 files changed

+253
-0
lines changed

6 files changed

+253
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package leetcode
2+
3+
func runningSum(nums []int) []int {
4+
result := []int{}
5+
counter := 0
6+
7+
for x := 0; x < len(nums); x++ {
8+
for y := 0; y < x; y++ {
9+
counter += nums[y]
10+
}
11+
12+
val := counter + nums[x]
13+
result = append(result, val)
14+
15+
counter = 0
16+
}
17+
18+
return result
19+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question1480 struct {
9+
para1480
10+
ans1480
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para1480 struct {
16+
nums []int
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans1480 struct {
22+
one []int
23+
}
24+
25+
func Test_Problem1480(t *testing.T) {
26+
27+
qs := []question1480{
28+
29+
{
30+
para1480{[]int{1, 2, 3, 4}},
31+
ans1480{[]int{1, 2, 6, 10}},
32+
},
33+
34+
{
35+
para1480{[]int{1, 1, 1, 1, 1}},
36+
ans1480{[]int{1, 2, 3, 4, 5}},
37+
},
38+
39+
{
40+
para1480{[]int{3, 1, 2, 10, 1}},
41+
ans1480{[]int{3, 4, 6, 16, 17}},
42+
},
43+
}
44+
45+
fmt.Printf("------------------------Leetcode Problem 1480------------------------\n")
46+
47+
for _, q := range qs {
48+
_, p := q.ans1480, q.para1480
49+
fmt.Printf("【input】:%v 【output】:%v \n", p, runningSum(p.nums))
50+
}
51+
fmt.Printf("\n\n\n")
52+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# [1480. Running Sum of 1d Array](https://leetcode.com/problems/running-sum-of-1d-array/)
2+
3+
## 题目
4+
5+
Given an array `nums`. We define a running sum of an array as `runningSum[i] = sum(nums[0]…nums[i])`.
6+
7+
Return the running sum of `nums`.
8+
9+
**Example 1**:
10+
11+
```
12+
Input: nums = [1,2,3,4]
13+
Output: [1,3,6,10]
14+
Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].
15+
16+
```
17+
18+
**Example 2**:
19+
20+
```
21+
Input: nums = [1,1,1,1,1]
22+
Output: [1,2,3,4,5]
23+
Explanation: Running sum is obtained as follows: [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1].
24+
25+
```
26+
27+
**Example 3**:
28+
29+
```
30+
Input: nums = [3,1,2,10,1]
31+
Output: [3,4,6,16,17]
32+
33+
```
34+
35+
**Constraints**:
36+
37+
- `1 <= nums.length <= 1000`
38+
- `-10^6 <= nums[i] <= 10^6`
39+
40+
```go
41+
func runningSum(nums []int) []int {
42+
result := []int{}
43+
counter := 0
44+
45+
for x := 0; x < len(nums); x++ {
46+
for y := 0; y < x; y++ {
47+
counter += nums[y]
48+
}
49+
50+
val := counter + nums[x]
51+
result = append(result, val)
52+
53+
counter = 0
54+
}
55+
56+
return result
57+
}
58+
59+
```
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package leetcode
2+
3+
func numIdenticalPairs(nums []int) int {
4+
total := 0
5+
for x := 0; x < len(nums); x++ {
6+
for y := x + 1; y < len(nums); y++ {
7+
if nums[x] == nums[y] {
8+
total++
9+
}
10+
}
11+
}
12+
13+
return total
14+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question1512 struct {
9+
para1512
10+
ans1512
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para1512 struct {
16+
nums []int
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans1512 struct {
22+
one int
23+
}
24+
25+
func Test_Problem1512(t *testing.T) {
26+
27+
qs := []question1512{
28+
29+
{
30+
para1512{[]int{1, 2, 3, 1, 1, 3}},
31+
ans1512{4},
32+
},
33+
34+
{
35+
para1512{[]int{1, 1, 1, 1}},
36+
ans1512{6},
37+
},
38+
39+
{
40+
para1512{[]int{1, 2, 3}},
41+
ans1512{0},
42+
},
43+
}
44+
45+
fmt.Printf("------------------------Leetcode Problem 1512------------------------\n")
46+
47+
for _, q := range qs {
48+
_, p := q.ans1512, q.para1512
49+
fmt.Printf("【input】:%v 【output】:%v \n", p, numIdenticalPairs(p.nums))
50+
}
51+
fmt.Printf("\n\n\n")
52+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# [1512. Number of Good Pairs](https://leetcode.com/problems/number-of-good-pairs/)
2+
3+
## 题目
4+
5+
Given an array of integers `nums`.
6+
7+
A pair `(i,j)` is called good if `nums[i] == nums[j]` and `i < j`.
8+
9+
Return the number of good pairs.
10+
11+
**Example 1**:
12+
13+
```
14+
Input: nums = [1,2,3,1,1,3]
15+
Output: 4
16+
Explanation: There are 4 good pairs (0,3), (0,4), (3,4), (2,5) 0-indexed.
17+
18+
```
19+
20+
**Example 2**:
21+
22+
```
23+
Input: nums = [1,1,1,1]
24+
Output: 6
25+
Explanation: Each pair in the array are good.
26+
27+
```
28+
29+
**Example 3**:
30+
31+
```
32+
Input: nums = [1,2,3]
33+
Output: 0
34+
35+
```
36+
37+
**Constraints**:
38+
39+
- `1 <= nums.length <= 1000`
40+
- `1 <= nums[i] <= 100`
41+
42+
```go
43+
func numIdenticalPairs(nums []int) int {
44+
total := 0
45+
for x := 0; x < len(nums); x++ {
46+
for y := x + 1; y < len(nums); y++ {
47+
if nums[x] == nums[y] {
48+
total++
49+
}
50+
}
51+
}
52+
53+
return total
54+
}
55+
56+
57+
```

0 commit comments

Comments
 (0)