leet-code/src/main/java/leetcode/editor/cn/BinarySearch.java
2021-07-06 16:41:28 +08:00

52 lines
1.3 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target ,写一个函数搜索 nums 中的 target如果目标值存在返回下标
//则返回 -1。
//
//
//示例 1:
//
// 输入: nums = [-1,0,3,5,9,12], target = 9
//输出: 4
//解释: 9 出现在 nums 中并且下标为 4
//
//
// 示例 2:
//
// 输入: nums = [-1,0,3,5,9,12], target = 2
//输出: -1
//解释: 2 不存在 nums 中因此返回 -1
//
//
//
//
// 提示:
//
//
// 你可以假设 nums 中的所有元素是不重复的。
// n 将在 [1, 10000]之间。
// nums 的每个元素都将在 [-9999, 9999]之间。
//
// Related Topics 数组 二分查找
// 👍 256 👎 0
package leetcode.editor.cn;
import java.util.Arrays;
//704:二分查找
public class BinarySearch {
public static void main(String[] args) {
//测试代码
Solution solution = new BinarySearch().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int search(int[] nums, int target) {
int result = Arrays.binarySearch(nums, target);
return result < 0 ? -1 : result;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}