File tree Expand file tree Collapse file tree 3 files changed +70
-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 +70
-0
lines changed Original file line number Diff line number Diff line change @@ -211,6 +211,31 @@ var searchRange = function (nums, target) {
211211};
212212```
213213
214+ #### C#
215+
216+ ``` cs
217+ public class Solution {
218+ public int [] SearchRange (int [] nums , int target ) {
219+ int l = Search (nums , target );
220+ int r = Search (nums , target + 1 );
221+ return l == r ? new int [] {- 1 , - 1 } : new int [] {l , r - 1 };
222+ }
223+
224+ private int Search (int [] nums , int x ) {
225+ int left = 0 , right = nums .Length ;
226+ while (left < right ) {
227+ int mid = (left + right ) >>> 1 ;
228+ if (nums [mid ] >= x ) {
229+ right = mid ;
230+ } else {
231+ left = mid + 1 ;
232+ }
233+ }
234+ return left ;
235+ }
236+ }
237+ ```
238+
214239#### PHP
215240
216241``` php
Original file line number Diff line number Diff line change @@ -201,6 +201,31 @@ var searchRange = function (nums, target) {
201201};
202202```
203203
204+ #### C#
205+
206+ ``` cs
207+ public class Solution {
208+ public int [] SearchRange (int [] nums , int target ) {
209+ int l = Search (nums , target );
210+ int r = Search (nums , target + 1 );
211+ return l == r ? new int [] {- 1 , - 1 } : new int [] {l , r - 1 };
212+ }
213+
214+ private int Search (int [] nums , int x ) {
215+ int left = 0 , right = nums .Length ;
216+ while (left < right ) {
217+ int mid = (left + right ) >>> 1 ;
218+ if (nums [mid ] >= x ) {
219+ right = mid ;
220+ } else {
221+ left = mid + 1 ;
222+ }
223+ }
224+ return left ;
225+ }
226+ }
227+ ```
228+
204229#### PHP
205230
206231``` php
Original file line number Diff line number Diff line change 1+ public class Solution {
2+ public int [ ] SearchRange ( int [ ] nums , int target ) {
3+ int l = Search ( nums , target ) ;
4+ int r = Search ( nums , target + 1 ) ;
5+ return l == r ? new int [ ] { - 1 , - 1 } : new int [ ] { l , r - 1 } ;
6+ }
7+
8+ private int Search ( int [ ] nums , int x ) {
9+ int left = 0 , right = nums . Length ;
10+ while ( left < right ) {
11+ int mid = ( left + right ) >>> 1 ;
12+ if ( nums [ mid ] >= x ) {
13+ right = mid ;
14+ } else {
15+ left = mid + 1 ;
16+ }
17+ }
18+ return left ;
19+ }
20+ }
You can’t perform that action at this time.
0 commit comments