Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitcf8bbcc

Browse files
authored
Merge pull requestneetcode-gh#2106 from a93a/main
Create 0034-find-first-and-last-position-of-element-in-sorted-array.kt
2 parents6b03699 +90ea69a commitcf8bbcc

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//same solution but with function
2+
classSolution {
3+
funsearchRange(nums:IntArray,target:Int):IntArray {
4+
val res= intArrayOf(-1,-1)
5+
6+
funbinarySearch(getLeft:Boolean):Int{
7+
var left=0
8+
var right= nums.size-1
9+
var pos=-1
10+
while(left<= right){
11+
val mid= (left+ right)/2
12+
if(target> nums[mid]){
13+
left= mid+1
14+
}elseif(target< nums[mid]){
15+
right= mid-1
16+
}else{
17+
pos= mid
18+
if(getLeft)
19+
right= mid-1
20+
else
21+
left= mid+1
22+
}
23+
}
24+
return pos
25+
}
26+
27+
res[0]= binarySearch(true)
28+
res[1]= binarySearch(false)
29+
30+
return res
31+
}
32+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp