Skip to content

Commit 0a31cd4

Browse files
committed
Leetcode solution Search Insert Position
1 parent 8cd7193 commit 0a31cd4

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

2023_year/February_2023/index11.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// https://leetcode.com/problems/search-insert-position/description/
2+
3+
/**
4+
* @param {number[]} nums
5+
* @param {number} target
6+
* @return {number}
7+
*/
8+
var searchInsert = function(nums, target) {
9+
// Initialize result variavle and set to zero
10+
let result = 0;
11+
// loop through the every element of an array
12+
for(let i= 0; i<nums.length; i++){
13+
// check if last value of an array is greater than or equal to target
14+
if(nums[nums.length -1] >= target){
15+
// check if current item is equal to target
16+
if(nums[i] === target){
17+
result = i;
18+
break;
19+
}else if(nums[i] > target){ // check if current item is greater then target
20+
result = i;
21+
break;
22+
}
23+
}else{
24+
// if last value of array is less than target
25+
result = nums.length;
26+
}
27+
}
28+
// check if result is zero the retutn array length value neighter result value
29+
return result;
30+
};
31+
console.log(searchInsert([1,3,5,6], 5)); // 2
32+
33+
/*
34+
Input: nums = [1,3,5,6], target = 5
35+
Output: 2
36+
*/

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
<body>
1010
<div id="root"></div>
1111

12-
<script src="./2023_year/February_2023/index10.js"></script>
12+
<script src="./2023_year/February_2023/index11.js"></script>
1313
</body>
1414
</html>

0 commit comments

Comments
 (0)