-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
39 additions
and
0 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
Medium/34. First and Last Position of Element in an Array/New Approach/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
public class Solution { | ||
public int[] searchRange(int[] nums, int target) { | ||
|
||
int n=nums.length; | ||
int arr[] =new int[2]; | ||
int arr1[] =new int[]{-1,-1}; | ||
arr[0] = -1; arr[1] = -1; | ||
for (int i = 0; i < n; i++) { | ||
if (target != nums[i]) | ||
continue; | ||
if (arr[0] == -1) | ||
arr[0] = i; | ||
arr[1] = i; | ||
} | ||
if (arr[0] != -1) { | ||
return arr; | ||
|
||
} | ||
else | ||
return arr1; | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
Medium/34. First and Last Position of Element in an Array/New Approach/sol.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
### `searchRange` Function Explanation | ||
|
||
1. Get the length of the input array `nums`. | ||
2. Initialize an array to store the search range. | ||
3. Initialize an array with [-1, -1] (default return value). | ||
4. Initialize the start index of the search range as -1. | ||
5. Initialize the end index of the search range as -1. | ||
6. Start iterating through the input array. | ||
7. If the current element is not the target, continue to the next element. | ||
8. If the current element is not the target, continue to the next iteration. | ||
9. If the start index of the search range is -1, set it to the current index. | ||
10. Update the end index of the search range to the current index when a match is found. | ||
11. If the start index is not -1, return the search range. | ||
12. Return the search range containing the start and end indices of the target. | ||
13. If no match was found, return the default [-1, -1] array. | ||
14. Return the default search range [-1, -1] to indicate no match. |