Skip to content

Commit fd9d3fd

Browse files
committed
Add solution 1539
1 parent 88f0a1d commit fd9d3fd

File tree

5 files changed

+195
-0
lines changed

5 files changed

+195
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package leetcode
2+
3+
func findKthPositive(arr []int, k int) int {
4+
positive, index := 1, 0
5+
for index < len(arr) {
6+
if arr[index] != positive {
7+
k--
8+
} else {
9+
index++
10+
}
11+
if k == 0 {
12+
break
13+
}
14+
positive++
15+
}
16+
if k != 0 {
17+
positive += k - 1
18+
}
19+
return positive
20+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question1539 struct {
9+
para1539
10+
ans1539
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para1539 struct {
16+
arr []int
17+
k int
18+
}
19+
20+
// ans 是答案
21+
// one 代表第一个答案
22+
type ans1539 struct {
23+
one int
24+
}
25+
26+
func Test_Problem1539(t *testing.T) {
27+
28+
qs := []question1539{
29+
30+
{
31+
para1539{[]int{2, 3, 4, 7, 11}, 5},
32+
ans1539{9},
33+
},
34+
35+
{
36+
para1539{[]int{1, 2, 3, 4}, 2},
37+
ans1539{6},
38+
},
39+
}
40+
41+
fmt.Printf("------------------------Leetcode Problem 1539------------------------\n")
42+
43+
for _, q := range qs {
44+
_, p := q.ans1539, q.para1539
45+
fmt.Printf("【input】:%v 【output】:%v \n", p, findKthPositive(p.arr, p.k))
46+
}
47+
fmt.Printf("\n\n\n")
48+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# [1539. Kth Missing Positive Number](https://leetcode.com/problems/kth-missing-positive-number/)
2+
3+
## 题目
4+
5+
Given an array `arr` of positive integers sorted in a **strictly increasing order**, and an integer `k`.
6+
7+
*Find the* `kth` *positive integer that is missing from this array.*
8+
9+
**Example 1:**
10+
11+
```
12+
Input: arr = [2,3,4,7,11], k = 5
13+
Output: 9
14+
Explanation: The missing positive integers are [1,5,6,8,9,10,12,13,...]. The 5th missing positive integer is 9.
15+
```
16+
17+
**Example 2:**
18+
19+
```
20+
Input: arr = [1,2,3,4], k = 2
21+
Output: 6
22+
Explanation: The missing positive integers are [5,6,7,...]. The 2nd missing positive integer is 6.
23+
```
24+
25+
**Constraints:**
26+
27+
- `1 <= arr.length <= 1000`
28+
- `1 <= arr[i] <= 1000`
29+
- `1 <= k <= 1000`
30+
- `arr[i] < arr[j]` for `1 <= i < j <= arr.length`
31+
32+
## 题目大意
33+
34+
给你一个 **严格升序排列** 的正整数数组 `arr` 和一个整数 `k` 。请你找到这个数组里第 `k` 个缺失的正整数。
35+
36+
## 解题思路
37+
38+
- 简单题。用一个变量从 1 开始累加,依次比对数组中是否存在,不存在的话就把 k - -,直到 k 为 0 的时候即是要输出的值。特殊情况,missing positive 都在数组之外,如例子 2 。
39+
40+
## 代码
41+
42+
```go
43+
package leetcode
44+
45+
func findKthPositive(arr []int, k int) int {
46+
positive, index := 1, 0
47+
for index < len(arr) {
48+
if arr[index] != positive {
49+
k--
50+
} else {
51+
index++
52+
}
53+
if k == 0 {
54+
break
55+
}
56+
positive++
57+
}
58+
if k != 0 {
59+
positive += k - 1
60+
}
61+
return positive
62+
}
63+
```
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# [1539. Kth Missing Positive Number](https://leetcode.com/problems/kth-missing-positive-number/)
2+
3+
## 题目
4+
5+
Given an array `arr` of positive integers sorted in a **strictly increasing order**, and an integer `k`.
6+
7+
*Find the* `kth` *positive integer that is missing from this array.*
8+
9+
**Example 1:**
10+
11+
```
12+
Input: arr = [2,3,4,7,11], k = 5
13+
Output: 9
14+
Explanation: The missing positive integers are [1,5,6,8,9,10,12,13,...]. The 5th missing positive integer is 9.
15+
```
16+
17+
**Example 2:**
18+
19+
```
20+
Input: arr = [1,2,3,4], k = 2
21+
Output: 6
22+
Explanation: The missing positive integers are [5,6,7,...]. The 2nd missing positive integer is 6.
23+
```
24+
25+
**Constraints:**
26+
27+
- `1 <= arr.length <= 1000`
28+
- `1 <= arr[i] <= 1000`
29+
- `1 <= k <= 1000`
30+
- `arr[i] < arr[j]` for `1 <= i < j <= arr.length`
31+
32+
## 题目大意
33+
34+
给你一个 **严格升序排列** 的正整数数组 `arr` 和一个整数 `k` 。请你找到这个数组里第 `k` 个缺失的正整数。
35+
36+
## 解题思路
37+
38+
- 简单题。用一个变量从 1 开始累加,依次比对数组中是否存在,不存在的话就把 k - -,直到 k 为 0 的时候即是要输出的值。特殊情况,missing positive 都在数组之外,如例子 2 。
39+
40+
## 代码
41+
42+
```go
43+
package leetcode
44+
45+
func findKthPositive(arr []int, k int) int {
46+
positive, index := 1, 0
47+
for index < len(arr) {
48+
if arr[index] != positive {
49+
k--
50+
} else {
51+
index++
52+
}
53+
if k == 0 {
54+
break
55+
}
56+
positive++
57+
}
58+
if k != 0 {
59+
positive += k - 1
60+
}
61+
return positive
62+
}
63+
```

website/content/menu/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,7 @@ headless: true
555555
- [1470.Shuffle-the-Array]({{< relref "/ChapterFour/1470.Shuffle-the-Array.md" >}})
556556
- [1480.Running-Sum-of-1d-Array]({{< relref "/ChapterFour/1480.Running-Sum-of-1d-Array.md" >}})
557557
- [1512.Number-of-Good-Pairs]({{< relref "/ChapterFour/1512.Number-of-Good-Pairs.md" >}})
558+
- [1539.Kth-Missing-Positive-Number]({{< relref "/ChapterFour/1539.Kth-Missing-Positive-Number.md" >}})
558559
- [1573.Number-of-Ways-to-Split-a-String]({{< relref "/ChapterFour/1573.Number-of-Ways-to-Split-a-String.md" >}})
559560
- [1640.Check-Array-Formation-Through-Concatenation]({{< relref "/ChapterFour/1640.Check-Array-Formation-Through-Concatenation.md" >}})
560561
- [1646.Get-Maximum-in-Generated-Array]({{< relref "/ChapterFour/1646.Get-Maximum-in-Generated-Array.md" >}})

0 commit comments

Comments
 (0)