Merge pull request #71 from harryleonardo/master

Add problem 1480 & 1512
This commit is contained in:
halfrost
2020-09-24 16:00:22 +08:00
committed by YDZ
9 changed files with 391 additions and 0 deletions

View File

@ -0,0 +1,10 @@
package leetcode
func runningSum(nums []int) []int {
dp := make([]int, len(nums)+1)
dp[0] = 0
for i := 1; i <= len(nums); i++ {
dp[i] = dp[i-1] + nums[i-1]
}
return dp[1:]
}

View File

@ -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, 3, 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")
}

View File

@ -0,0 +1,64 @@
# [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`
## 题目大意
给你一个数组 nums 。数组「动态和」的计算公式为runningSum[i] = sum(nums[0]…nums[i]) 。请返回 nums 的动态和。
## 解题思路
- 简单题,按照题意依次循环计算前缀和即可。
## 代码
```go
package leetcode
func runningSum(nums []int) []int {
dp := make([]int, len(nums)+1)
dp[0] = 0
for i := 1; i <= len(nums); i++ {
dp[i] = dp[i-1] + nums[i-1]
}
return dp[1:]
}
```

View File

@ -0,0 +1,13 @@
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
}

View File

@ -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")
}

View File

@ -0,0 +1,67 @@
# [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`
## 题目大意
给你一个整数数组 nums。如果一组数字 (i,j) 满足 nums[i] == nums[j] 且 i < j 就可以认为这是一组好数对返回好数对的数目
## 解题思路
- 简单题按照题目中好数对的定义循环遍历判断两数是否相等累加计数即可
## 代码
```go
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
}
```