diff --git a/leetcode/1901-2000/1920.Build-Array-from-Permutation/README.md b/leetcode/1901-2000/1920.Build-Array-from-Permutation/README.md index 211c57b9a..37619b1ca 100755 --- a/leetcode/1901-2000/1920.Build-Array-from-Permutation/README.md +++ b/leetcode/1901-2000/1920.Build-Array-from-Permutation/README.md @@ -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] +``` ## 结语 diff --git a/leetcode/1901-2000/1920.Build-Array-from-Permutation/Solution.go b/leetcode/1901-2000/1920.Build-Array-from-Permutation/Solution.go index d115ccf5e..fcea94fe2 100644 --- a/leetcode/1901-2000/1920.Build-Array-from-Permutation/Solution.go +++ b/leetcode/1901-2000/1920.Build-Array-from-Permutation/Solution.go @@ -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 } diff --git a/leetcode/1901-2000/1920.Build-Array-from-Permutation/Solution_test.go b/leetcode/1901-2000/1920.Build-Array-from-Permutation/Solution_test.go index 14ff50eb4..5bf1d5ab3 100644 --- a/leetcode/1901-2000/1920.Build-Array-from-Permutation/Solution_test.go +++ b/leetcode/1901-2000/1920.Build-Array-from-Permutation/Solution_test.go @@ -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}}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }