二分查找
二分查找
视频教程:https://www.bilibili.com/video/BV1wA411b7qZ?p=50&vd_source=1617b95d104fded302dc89bd7ea2630a
在开发中,我们经常会需要对数据进行转化处理,在JavaScript中提供了两个函数:**JSON.parse()和JSON.stringify()**。
题目
给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target ,写一个函数搜索 nums 中的 target,如果目标值存在返回下标,否则返回 -1。
示例 1:
输入: nums = [-1,0,3,5,9,12], target = 9
输出: 4
解释: 9 出现在 nums 中并且下标为 4
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/binary-search
解题:
1 | var search = function (nums,target) { |
All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
Comment