Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[ADD] 1480 & 1512
  • Loading branch information
Harry Leonardo authored and Harry Leonardo committed Sep 23, 2020
commit 44f8b5b8a6e209b19b4e7fd326848a6e14c33919
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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")
}
59 changes: 59 additions & 0 deletions leetcode/1480.Running-Sum-of-1d-Array/README.md
Original file line number Diff line number Diff line change
@@ -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
}

```
14 changes: 14 additions & 0 deletions leetcode/1512.Number-of-Good-Pairs/1512.Number-of-Good-Pairs.go
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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")
}
57 changes: 57 additions & 0 deletions leetcode/1512.Number-of-Good-Pairs/README.md
Original file line number Diff line number Diff line change
@@ -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
}


```