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

Commit 6d341cd

Browse files
✅ NEW: Array Search Insert Position Solved
1 parent 00c5383 commit 6d341cd

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ npm test
1212
1. [Two Sum](https://leetcode.com/problems/two-sum/) - [Solution](https://github.com/ManiruzzamanAkash/LeetCode-In-JS/blob/main/two-sum/)
1313
1. [Remove duplicates from sorted array]() - [Solution](https://github.com/ManiruzzamanAkash/LeetCode-In-JS/tree/main/remove-duplicates-from-sorted-array)
1414
1. [Remove elements from array](https://leetcode.com/problems/remove-element) - [Solution](https://github.com/ManiruzzamanAkash/LeetCode-In-JS/tree/main/remove-element)
15+
1. [Search Insert Position](https://leetcode.com/problems/search-insert-position) - [Solution](https://github.com/ManiruzzamanAkash/LeetCode-In-JS/tree/main/search-insert-position)
1516

1617
## String Problems Solution -
1718
1. [Check Palindrome Number](https://leetcode.com/problems/palindrome-number/) - [Solution](https://github.com/ManiruzzamanAkash/LeetCode-In-JS/tree/main/palindrome-number)

‎search-insert-position/index.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Remove an element from the array.
3+
*
4+
* @param {number[]} nums
5+
* @param {number} target
6+
*
7+
* @return {number}
8+
*/
9+
var searchInsert = function(nums, target) {
10+
for (let i = 0; i < nums.length; i++) {
11+
const num = nums[i];
12+
13+
if (num == target || num > target) { // two cases
14+
return parseInt(i);
15+
} else if (typeof nums[i + 1] !== "undefined" && nums[i + 1] > target) {
16+
return parseInt(i + 1);
17+
}
18+
}
19+
20+
return nums.length;
21+
};
22+
23+
module.exports = searchInsert;

‎search-insert-position/index.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const searchInsert = require("./index");
2+
3+
test("[1,3,5,6] array and target=5 would result 2", () => {
4+
expect(searchInsert([1, 3, 5, 6], 5)).toStrictEqual(2);
5+
});
6+
7+
test("[1,3,5,6] array and target=2 would result 1", () => {
8+
expect(searchInsert([1, 3, 5, 6], 2)).toStrictEqual(1);
9+
});
10+
11+
test("[1,3,5,6] array and target=7 would result 4", () => {
12+
expect(searchInsert([1, 3, 5, 6], 7)).toStrictEqual(4);
13+
});
14+
15+
test("[1,3,5,6] array and target=0 would result 0", () => {
16+
expect(searchInsert([1, 3, 5, 6], 0)).toStrictEqual(0);
17+
});

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /