Skip to content

Commit b3f482f

Browse files
committed
Add solution and test-cases for problem 3159
1 parent f03226b commit b3f482f

File tree

3 files changed

+49
-26
lines changed

3 files changed

+49
-26
lines changed

leetcode/3101-3200/3159.Find-Occurrences-of-an-Element-in-an-Array/README.md

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,38 @@
11
# [3159.Find Occurrences of an Element in an Array][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given an integer array `nums`, an integer array `queries`, and an integer `x`.
5+
6+
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.
7+
8+
Return an integer array `answer` containing the answers to all queries.
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
13+
Input: nums = [1,3,1,7], queries = [1,3,2,4], x = 1
14+
15+
Output: [0,-1,2,-1]
1416
15-
## 题意
16-
> ...
17+
Explanation:
1718
18-
## 题解
19+
For the 1st query, the first occurrence of 1 is at index 0.
20+
For the 2nd query, there are only two occurrences of 1 in nums, so the answer is -1.
21+
For the 3rd query, the second occurrence of 1 is at index 2.
22+
For the 4th query, there are only two occurrences of 1 in nums, so the answer is -1.
23+
```
24+
25+
**Example 2:**
1926

20-
### 思路1
21-
> ...
22-
Find Occurrences of an Element in an Array
23-
```go
2427
```
28+
Input: nums = [1,2,3], queries = [10], x = 5
29+
30+
Output: [-1]
2531
32+
Explanation:
33+
34+
For the 1st query, 5 doesn't exist in nums, so the answer is -1.
35+
```
2636

2737
## 结语
2838

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int, queries []int, x int) []int {
4+
indies := []int{}
5+
for idx := 0; idx < len(nums); idx++ {
6+
if nums[idx] == x {
7+
indies = append(indies, idx)
8+
}
9+
}
10+
ret := make([]int, len(queries))
11+
for idx, q := range queries {
12+
ret[idx] = -1
13+
if q <= len(indies) {
14+
ret[idx] = indies[q-1]
15+
}
16+
}
17+
return ret
518
}

leetcode/3101-3200/3159.Find-Occurrences-of-an-Element-in-an-Array/Solution_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,31 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
nums, queries []int
14+
x int
15+
expect []int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{1, 3, 1, 7}, []int{1, 3, 2, 4}, 1, []int{0, -1, 2, -1}},
18+
{"TestCase2", []int{1, 2, 3}, []int{10}, 5, []int{-1}},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.nums, c.queries, c.x)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
27+
c.expect, got, c.nums, c.queries, c.x)
2828
}
2929
})
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)