diff --git a/leetcode/3101-3200/3159.Find-Occurrences-of-an-Element-in-an-Array/README.md b/leetcode/3101-3200/3159.Find-Occurrences-of-an-Element-in-an-Array/README.md index 8e248e092..76b86a5f7 100755 --- a/leetcode/3101-3200/3159.Find-Occurrences-of-an-Element-in-an-Array/README.md +++ b/leetcode/3101-3200/3159.Find-Occurrences-of-an-Element-in-an-Array/README.md @@ -1,28 +1,38 @@ # [3159.Find Occurrences of an Element in an Array][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 +You are given an integer array `nums`, an integer array `queries`, and an integer `x`. + +For each `queries[i]`, you need to find the index of the `queries[i]th` occurrence of `x` in the `nums` array. If there are fewer than `queries[i]` occurrences of `x`, the answer should be -1 for that query. + +Return an integer array `answer` containing the answers to all queries. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" -``` +Input: nums = [1,3,1,7], queries = [1,3,2,4], x = 1 + +Output: [0,-1,2,-1] -## 题意 -> ... +Explanation: -## 题解 +For the 1st query, the first occurrence of 1 is at index 0. +For the 2nd query, there are only two occurrences of 1 in nums, so the answer is -1. +For the 3rd query, the second occurrence of 1 is at index 2. +For the 4th query, there are only two occurrences of 1 in nums, so the answer is -1. +``` + +**Example 2:** -### 思路1 -> ... -Find Occurrences of an Element in an Array -```go ``` +Input: nums = [1,2,3], queries = [10], x = 5 + +Output: [-1] +Explanation: + +For the 1st query, 5 doesn't exist in nums, so the answer is -1. +``` ## 结语 diff --git a/leetcode/3101-3200/3159.Find-Occurrences-of-an-Element-in-an-Array/Solution.go b/leetcode/3101-3200/3159.Find-Occurrences-of-an-Element-in-an-Array/Solution.go index d115ccf5e..229d27d69 100644 --- a/leetcode/3101-3200/3159.Find-Occurrences-of-an-Element-in-an-Array/Solution.go +++ b/leetcode/3101-3200/3159.Find-Occurrences-of-an-Element-in-an-Array/Solution.go @@ -1,5 +1,18 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(nums []int, queries []int, x int) []int { + indies := []int{} + for idx := 0; idx < len(nums); idx++ { + if nums[idx] == x { + indies = append(indies, idx) + } + } + ret := make([]int, len(queries)) + for idx, q := range queries { + ret[idx] = -1 + if q <= len(indies) { + ret[idx] = indies[q-1] + } + } + return ret } diff --git a/leetcode/3101-3200/3159.Find-Occurrences-of-an-Element-in-an-Array/Solution_test.go b/leetcode/3101-3200/3159.Find-Occurrences-of-an-Element-in-an-Array/Solution_test.go index 14ff50eb4..01d7fb339 100644 --- a/leetcode/3101-3200/3159.Find-Occurrences-of-an-Element-in-an-Array/Solution_test.go +++ b/leetcode/3101-3200/3159.Find-Occurrences-of-an-Element-in-an-Array/Solution_test.go @@ -9,31 +9,31 @@ import ( func TestSolution(t *testing.T) { // 测试用例 cases := []struct { - name string - inputs bool - expect bool + name string + nums, queries []int + x int + expect []int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{1, 3, 1, 7}, []int{1, 3, 2, 4}, 1, []int{0, -1, 2, -1}}, + {"TestCase2", []int{1, 2, 3}, []int{10}, 5, []int{-1}}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.nums, c.queries, c.x) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v", + c.expect, got, c.nums, c.queries, c.x) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }