
二分查找
wutingyehe
Just for fun
展开
-
LintCode Binary Search 二分查找
给定一个排序的整数数组(升序)和一个要查找的整数target,用O(logn)的时间查找到target第一次出现的下标(从0开始),如果target不存在于数组中,返回-1。For a given sorted array (ascending order) and a target number, find the first index of this number in O(log n) ti原创 2015-07-10 17:51:37 · 1312 阅读 · 0 评论 -
LintCode Merge Sorted Array 合并排序数组
合并两个排序的整数数组A和B变成一个新的数组。 Merge two given sorted integer array A and B into a new sorted integer array.样例 给出A=[1,2,3,4],B=[2,4,5,6],返回 [1,2,2,3,4,4,5,6]挑战 你能否优化你的算法,如果其中一个数组很大而另一个数组很小? How can you op原创 2015-07-14 16:00:41 · 1110 阅读 · 0 评论 -
【LintCode】Search Insert Position 搜索插入位置
给定一个排序数组和一个目标值,如果在数组中找到目标值则返回索引。如果没有,返回到它将会被按顺序插入的位置。 你可以假设在数组中无重复元素。样例 [1,3,5,6],5 → 2 [1,3,5,6],2 → 1 [1,3,5,6], 7 → 4 [1,3,5,6],0 → 0public class Solution { /** * param A : an integer原创 2015-07-15 22:07:17 · 1909 阅读 · 0 评论 -
【LintCode】 Find Minimum in Rotated Sorted Array 寻找旋转排序数组中的最小值
假设一个旋转排序的数组其起始位置是未知的(比如0 1 2 4 5 6 7 可能变成是4 5 6 7 0 1 2)。 你需要找到其中最小的元素。 你可以假设数组中不存在重复的元素。样例 给出[4,5,6,7,0,1,2] 返回 0public class Solution { /** * @param num: a rotated sorted array * @r原创 2015-07-15 22:53:35 · 572 阅读 · 0 评论 -
【LintCode】Insert Intervals 插入区间
给出一个无重叠的按照区间起始端点排序的区间列表。 在列表中插入一个新的区间,你要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间)。样例 插入区间[2, 5] 到 [[1,2], [5,9]],我们得到 [[1,9]]。 插入区间[3, 4] 到 [[1,2], [5,9]],我们得到 [[1,2], [3,4], [5,9]]。/** * Definition of Inte原创 2015-07-16 10:56:53 · 1352 阅读 · 0 评论 -
[LintCode]搜索二维矩阵 II
写出一个高效的算法来搜索m×n矩阵中的值,返回这个值出现的次数。 这个矩阵具有以下特性: 每行中的整数从左到右是排序的。 每一列的整数从上到下是排序的。 在每一行或每一列中没有重复的整数。样例 考虑下列矩阵: [ [1, 3, 5, 7], [2, 4, 7, 8], [3, 5, 9, 10] ] 给出target = 3,返回 2挑战 要求O(m+原创 2016-02-04 17:45:14 · 902 阅读 · 0 评论 -
[LintCode] 搜索旋转排序数组 Search in Rotated Sorted Array
假设有一个排序的按未知的旋转轴旋转的数组(比如,0 1 2 4 5 6 7 可能成为4 5 6 7 0 1 2)。给定一个目标值进行搜索,如果在数组中找到目标值返回数组中的索引位置,否则返回-1。 你可以假设数组中不存在重复的元素。样例 给出[4, 5, 1, 2, 3]和target=1,返回 2 给出[4, 5, 1, 2, 3]和target=0,返回 -1public class So原创 2016-04-06 10:43:01 · 471 阅读 · 0 评论