Skip to content

Commit

Permalink
Merge pull request #549 from 0xff-dev/1920
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 1920
  • Loading branch information
6boris authored Aug 3, 2023
2 parents a35b885 + 963b5c7 commit cb54981
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 23 deletions.
31 changes: 17 additions & 14 deletions leetcode/1901-2000/1920.Build-Array-from-Permutation/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
# [1920.Build Array from Permutation][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
## Description
Given a **zero-based permutation** nums (**0-indexed**), build an array ans of the **same length** where `ans[i] = nums[nums[i]]` for each `0 <= i < nums.length` and return it.

A **zero-based permutation** `nums` is an array of **distinct** integers from `0` to `nums.length - 1` (**inclusive**).

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: nums = [0,2,1,5,3,4]
Output: [0,1,2,4,5,3]
Explanation: The array ans is built as follows:
ans = [nums[nums[0]], nums[nums[1]], nums[nums[2]], nums[nums[3]], nums[nums[4]], nums[nums[5]]]
= [nums[0], nums[2], nums[1], nums[5], nums[3], nums[4]]
= [0,1,2,4,5,3]
```

## 题意
> ...
## 题解
**Example 2:**

### 思路1
> ...
Build Array from Permutation
```go
```

Input: nums = [5,0,1,2,3,4]
Output: [4,5,0,1,2,3]
Explanation: The array ans is built as follows:
ans = [nums[nums[0]], nums[nums[1]], nums[nums[2]], nums[nums[3]], nums[nums[4]], nums[nums[5]]]
= [nums[5], nums[0], nums[1], nums[2], nums[3], nums[4]]
= [4,5,0,1,2,3]
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(nums []int) []int {
ans := make([]int, len(nums))
for idx, item := range nums {
ans[idx] = nums[item]
}
return ans
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs []int
expect []int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{0, 2, 1, 5, 3, 4}, []int{0, 1, 2, 4, 5, 3}},
{"TestCase2", []int{5, 0, 1, 2, 3, 4}, []int{4, 5, 0, 1, 2, 3}},
}

// 开始测试
Expand All @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}

0 comments on commit cb54981

Please sign in to comment.