File tree Expand file tree Collapse file tree 3 files changed +73
-0
lines changed
solution/0000-0099/0034.Find First and Last Position of Element in Sorted Array Expand file tree Collapse file tree 3 files changed +73
-0
lines changed Original file line number Diff line number Diff line change @@ -289,6 +289,32 @@ class Solution {
289
289
}
290
290
```
291
291
292
+ #### Kotlin
293
+
294
+ ``` kotlin
295
+ class Solution {
296
+ fun searchRange (nums : IntArray , target : Int ): IntArray {
297
+ val left = this .search(nums, target)
298
+ val right = this .search(nums, target + 1 )
299
+ return if (left == right) intArrayOf(- 1 , - 1 ) else intArrayOf(left, right - 1 )
300
+ }
301
+
302
+ private fun search (nums : IntArray , target : Int ): Int {
303
+ var left = 0
304
+ var right = nums.size
305
+ while (left < right) {
306
+ val middle = (left + right) / 2
307
+ if (nums[middle] < target) {
308
+ left = middle + 1
309
+ } else {
310
+ right = middle
311
+ }
312
+ }
313
+ return left
314
+ }
315
+ }
316
+ ```
317
+
292
318
<!-- tabs: end -->
293
319
294
320
<!-- solution: end -->
Original file line number Diff line number Diff line change @@ -279,6 +279,32 @@ class Solution {
279
279
}
280
280
```
281
281
282
+ #### Kotlin
283
+
284
+ ``` kotlin
285
+ class Solution {
286
+ fun searchRange (nums : IntArray , target : Int ): IntArray {
287
+ val left = this .search(nums, target)
288
+ val right = this .search(nums, target + 1 )
289
+ return if (left == right) intArrayOf(- 1 , - 1 ) else intArrayOf(left, right - 1 )
290
+ }
291
+
292
+ private fun search (nums : IntArray , target : Int ): Int {
293
+ var left = 0
294
+ var right = nums.size
295
+ while (left < right) {
296
+ val middle = (left + right) / 2
297
+ if (nums[middle] < target) {
298
+ left = middle + 1
299
+ } else {
300
+ right = middle
301
+ }
302
+ }
303
+ return left
304
+ }
305
+ }
306
+ ```
307
+
282
308
<!-- tabs: end -->
283
309
284
310
<!-- solution: end -->
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ fun searchRange (nums : IntArray , target : Int ): IntArray {
3
+ val left = this .search(nums, target)
4
+ val right = this .search(nums, target + 1 )
5
+ return if (left == right) intArrayOf(- 1 , - 1 ) else intArrayOf(left, right - 1 )
6
+ }
7
+
8
+ private fun search (nums : IntArray , target : Int ): Int {
9
+ var left = 0
10
+ var right = nums.size
11
+ while (left < right) {
12
+ val middle = (left + right) / 2
13
+ if (nums[middle] < target) {
14
+ left = middle + 1
15
+ } else {
16
+ right = middle
17
+ }
18
+ }
19
+ return left
20
+ }
21
+ }
You can’t perform that action at this time.
0 commit comments