8000 Added search.js and solution.js · jonasraoni/leetcode@65f401c · GitHub
[go: up one dir, main page]

Skip to content

Commit 65f401c

Browse files
committed
Added search.js and solution.js
1 parent af28b35 commit 65f401c

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed

easy/search.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//+ Jonas Raoni Soares Silva
2+
//@ http://raoni.org
3+
4+
/**
5+
* @param {number[]} nums
6+
* @param {number} target
7+
* @return {number}
8+
*/
9+
var search = function(nums, target) {
10+
let h = nums.length;
11+
for (let l = -1, m; h - l > 1; nums[m = l + (h - l >>> 1)] < target ? l = m : h = m);
12+
return nums[h] !== target ? -1 : h;
13+
};

easy/search.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# [704. Binary Search](https://leetcode.com/problems/binary-search)
2+
3+
Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1.
4+
5+
You must write an algorithm with O(log n) runtime complexity.
6+
7+
8+
9+
Example 1:
10+
11+
Input: nums = [-1,0,3,5,9,12], target = 9
12+
Output: 4
13+
Explanation: 9 exists in nums and its index is 4
14+
Example 2:
15+
16+
Input: nums = [-1,0,3,5,9,12], target = 2
17+
Output: -1
18+
Explanation: 2 does not exist in nums so return -1
19+
20+
21+
Constraints:
22+
23+
1 <= nums.length <= 104
24+
-104 < nums[i], target < 104
25+
All the integers in nums are unique.
26+
nums is sorted in ascending order.

easy/solution.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//+ Jonas Raoni Soares Silva
2+
//@ http://raoni.org
3+
4+
/**
5+
* Definition for isBadVersion()
6+
*
7+
* @param {integer} version number
8+
* @return {boolean} whether the version is bad
9+
* isBadVersion = function(version) {
10+
* ...
11+
* };
12+
*/
13+
14+
/**
15+
* @param {function} isBadVersion()
16+
* @return {function}
17+
*/
18+
var solution = function(isBadVersion) {
19+
/**
20+
* @param {integer} n Total versions
21+
* @return {integer} The first bad version
22+
*/
23+
return function(h) {
24+
for (let l = -1, m; h - l > 1; isBadVersion(m = l + (h - l >>> 1)) ? h = m : l = m);
25+
return h;
26+
};
27+
};

easy/solution.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# [278. First Bad Version](https://leetcode.com/problems/first-bad-version)
2+
3+
You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.
4+
5+
Suppose you have n versions [1, 2, ..., n] and you want to find out the first bad one, which causes all the following ones to be bad.
6+
7+
You are given an API bool isBadVersion(version) which returns whether version is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.
8+
9+
10+
11+
Example 1:
12+
13+
Input: n = 5, bad = 4
14+
Output: 4
15+
Explanation:
16+
call isBadVersion(3) -> false
17+
call isBadVersion(5) -> true
18+
call isBadVersion(4) -> true
19+
Then 4 is the first bad version.
20+
Example 2:
21+
22+
Input: n = 1, bad = 1
23+
Output: 1
24+
25+
26+
Constraints:
27+
28+
1 <= bad <= n <= 231 - 1

0 commit comments

Comments
 (0)
0