【LeetCode】 Binary Search 二分搜索
来源:互联网 发布:易语言dnf辅助源码模板 编辑:程序博客网 时间:2024/09/21 06:18
- For a given sorted array (ascending order) and a target number, find the first index of this number in O(log n) time complexity.
If the target number does not exist in the array, return -1.
Example :If the array is [1, 2, 3, 3, 4, 5, 10], for given target 3, return 2
(1)Java
// version 1: with template//template:①用while易死循环,若写得好可以给面试加分 ② while (start + 1 < end):相邻就退出,不容易死循环;至于最后:依据题目要找first/last position,相应的作出判断谁先谁后。【相应先判断:if (nums[start/last] == target) 】③取中位数写法好处:若整数太大,start+end可能溢出。class Solution { /** * @param nums: The integer array. * @param target: Target to find. * @return: The first position of target. Position starts from 0. */ public int binarySearch(int[] nums, int target) { if (nums == null || nums.length == 0) { return -1; } int start = 0, end = nums.length - 1; while (start + 1 < end) { int mid = start + (end - start) / 2 ③; if (nums[mid] == target) { end = mid; } else if (nums[mid] < target) { start = mid; // or start = mid + 1 } else { end = mid; // or end = mid - 1 } } if (nums[start] == target) { return start; } if (nums[end] == target) { return end; } return -1; }}// version 2: class Solution { public int binarySearch(int[] nums, int target) { if (nums == null || nums.length == 0) { return -1; } int start = 0, end = nums.length - 1; while (start < end) { int mid = start + (end - start) / 2; if (nums[mid] == target) { end = mid; } else if (nums[mid] < target) { start = mid + 1; } else { end = mid - 1; } } if (nums[start] == target) { return start; } return -1; }}
(2)C++
#include <vector>using namespace std;class Solution { public: /** * @param array source array * @param target target to search * @return the first occurrence position of target */ int binarySearch(vector<int> &A, int target) { if (A.size() == 0) { return -1; } int start = 0; int end = A.size() - 1; int mid; while (start + 1 < end) { mid = start + (end - start) / 2; if (A[mid] == target) { end = mid; } else if (A[mid] < target) { start = mid; } else if (A[mid] > target) { end = mid; } } if (A[start] == target) { return start; } if (A[end] == target) { return end; } return -1; }};
阅读全文
2 0
- 【LeetCode】 Binary Search 二分搜索
- 二分搜索 (Binary Search)
- 模板:Binary Search - 二分搜索
- Binary Search 二分查找,二分搜索 C++
- 二分搜索树(Binary search tree)
- 第五章 ALDS1_4_B Binary Search 二分搜索
- leetcode-二分搜索:Search Insert Position
- (LeetCode)Search Insert Position --- 二分搜索
- leetcode——Search Insert Position 二分查找 binary search
- 108.leetcode Convert Sorted Array to Binary Search Tree(medium)[数组 二分查找 二叉平衡搜索树]
- 109.leetcode Convert Sorted List to Binary Search Tree(medium)[链表 平衡二叉搜索树 二分查找]
- 二分搜索树排序binary search sorting的c++实现
- 二分搜索(binary search)简单实现 (迭代法)
- Unique Binary Search Trees 计算二分搜索树的个数
- Recover Binary Search Tree 恢复二分搜索树
- Leetcode:475. Heaters (利用二分binary search)
- 二分查找(Binary Search)
- binary-search( 二分查找 )
- 170923_Spring英文文档阅读(十四)_2.3 Usage scenarios_Dependency Management and Naming Conventions(一)
- 第三方开发者模式
- ThinkPHP 图片上传 改路径上传
- Dev 配置checkedListBoxControl值
- oj 序列判断
- 【LeetCode】 Binary Search 二分搜索
- CSS(二)— 样式加载
- 我的第一个网页爬虫(python)
- 解析enable_shared_from_this
- VPS搭建SS
- Android——蓝牙传输
- 欢迎使用CSDN-markdown编辑器
- python3 爬取妹子图。。。
- Proc 文件系统实现原理