二分法
六种鱼
这个作者很懒,什么都没留下…
展开
-
LintCode Find the Duplicate Number
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, fin原创 2017-10-23 14:25:59 · 229 阅读 · 0 评论 -
LintCode 最大平均值子数组
给出一个整数数组,有正有负。找到这样一个子数组,他的长度大于等于 k,且平均值最大。注意事项保证数组的大小 >= k样例 给出 nums = [1, 12, -5, -6, 50, 3], k = 3返回 15.667 // (-6 + 50 + 3) / 3 = 15.667参考了别人的想法,利用二分法,依然不是对数组中元素的二分,而是对一个区间的二分,逐渐找到结果。题目所求平均值一定是在 数组原创 2017-10-23 17:10:46 · 2842 阅读 · 0 评论 -
LintCode 搜索区间
给定一个包含 n 个整数的排序数组,找出给定目标值 target 的起始和结束位置。如果目标值不在数组中,则返回[-1, -1]样例 给出[5, 7, 7, 8, 8, 10]和目标值target=8,返回[3, 4]挑战 时间复杂度 O(log n)分别对左右边界进行二分查找。注意特殊情况的处理。直接看代码:public class Solution { /* * @par原创 2017-10-24 13:38:17 · 303 阅读 · 0 评论