Half and Half 类型题

二分法的精髓在于判断目标值在前半区间还是后半区间,Half and Half类型难点在不能一次判断,可能需要一次以上的判断条件。

Maximum Number in Mountain Sequence

Given a mountain sequence of n integers which increase firstly and then decrease, find the mountain top.

样例  Given nums = [1, 2, 4, 8, 6, 3] return 8   Given nums = [10, 9, 8, 7], return 10
public int mountainSequence(int[] nums) {
// write your code here
if(nums == null || nums.length == 0){
return -1;
}
int start = 0;
int end = nums.length - 1;
while(start + 1 < end){
int mid = start + (end - start)/2;
if(nums[start] < nums[mid]){
if(nums[mid+1]<nums[mid]){
end = mid;
}
else{
start = mid;
} }
else{
if(nums[mid-1]<nums[mid]){
start = mid;
}
else{
end = mid;
} }
}
if(nums[start] > nums[end]){
return nums[start];
}
else{
return nums[end];
}
//return -1;
}

假设有一个排序的按未知的旋转轴旋转的数组(比如,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,返回 -1

思路:判断目标值是否在某一区间/跨区间,再比较目标值

public int search(int[] A, int target) {
// write your code here
if(A == null | A.length == 0){
return -1;
}
int start = 0;
int end = A.length - 1;
while(start + 1 < end){
int mid = start + (end - start)/2;
if (A[start] < A[mid]){
if(target >= A[start] && target <= A[mid]){
end = mid;
}
else{
start = mid;
} }
else{
if(target >= A[mid] && target <= A[end]){
start = mid;
}
else{
end = mid;
}
}
}
if(A[start] == target){
return start;
}
if(A[end] == target){
return end;
}
return -1;
}
 

【lintcode】二分法总结 II的更多相关文章

  1. Lintcode: Sort Colors II 解题报告

    Sort Colors II 原题链接: http://lintcode.com/zh-cn/problem/sort-colors-ii/# Given an array of n objects ...

  2. Lintcode: Majority Number II 解题报告

    Majority Number II 原题链接: http://lintcode.com/en/problem/majority-number-ii/# Given an array of integ ...

  3. [LintCode] Wiggle Sort II 扭动排序之二

    Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]... ...

  4. [LintCode] Paint House II 粉刷房子之二

    There are a row of n houses, each house can be painted with one of the k colors. The cost of paintin ...

  5. [LintCode] House Robber II 打家劫舍之二

    After robbing those houses on that street, the thief has found himself a new place for his thievery ...

  6. lintcode:背包问题II

    背包问题II 给出n个物品的体积A[i]和其价值V[i],将他们装入一个大小为m的背包,最多能装入的总价值有多大? 注意事项 A[i], V[i], n, m均为整数.你不能将物品进行切分.你所挑选的 ...

  7. lintcode:排颜色 II

    排颜色 II 给定一个有n个对象(包括k种不同的颜色,并按照1到k进行编号)的数组,将对象进行分类使相同颜色的对象相邻,并按照1,2,...k的顺序进行排序. 样例 给出colors=[3, 2, 2 ...

  8. lintcode: 跳跃游戏 II

    跳跃游戏 II 给出一个非负整数数组,你最初定位在数组的第一个位置. 数组中的每个元素代表你在那个位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 样例 给出数组A =  ...

  9. Lintcode: k Sum II

    Given n unique integers, number k (1<=k<=n) and target. Find all possible k integers where the ...

随机推荐

  1. web开发前端面试知识点目录整理

    web开发前端面试知识点目录整理 基本功考察 关于Html 1. html语义化标签的理解; 结构化的理解; 能否写出简洁的html结构; SEO优化 2. h5中新增的属性; 如自定义属性data, ...

  2. OGG 18.1 for mysql远程捕获测试

    Ogg18.1 remote capture要求mysql为5.7版本,只能从linux远程捕获mysql on windows or linux,且不支持DDL捕获.支持远程mysql为commun ...

  3. electron 打包流程 electron-packager + NSIS

    1.安装 electron-packager 2.electron-packager 应用目录 应用名称 打包平台  左上角的图标和任务栏的图标  输出目录 架构 版本     win打包:  ele ...

  4. Pandas:深市股票代码前补足0

    #深市代码前补充0----------------- df[' #先增加一列 #将2列合并为新列 df['代码合并'] = df['补充'] + df['股票代码'] #再取后6位 df['股票代码' ...

  5. 无序hashset与hashmap让其有序

    今天迭代hashmap时,hashmap并不能按照put的顺序,迭代输出值.用下述方法可以: HashMap<String,String> hashmap = new LinkedHash ...

  6. [Python]基础教程(4)、Python 变量类型

    Python 变量类型 变量存储在内存中的值.这就意味着在创建变量时会在内存中开辟一个空间. 基于变量的数据类型,解释器会分配指定内存,并决定什么数据可以被存储在内存中. 因此,变量可以指定不同的数据 ...

  7. linux服务基础(一)之CentOS6编译安装httpd2.4

    安装http-2.4 Http依赖于apr-1.4+,apr-util-1.4+ CentOS6上默认是apr-1.3,apr-util1.3 先编译安装apr-1.5,apr-util-1.5 开始 ...

  8. 在Linux(Debian)环境下搭建并运行GPU

    首先通过以下命令查看是否GPU驱动成功: 注意:需要在bash终端输入 import tensorflow as tf hello = tf.constant('Hello, TensorFlow!' ...

  9. Lintcode487-Name Deduplication-Easy

    487. Name Deduplication Given a list of names, remove the duplicate names. Two name will be treated ...

  10. scrapy爬虫具体案例步骤详细分析

    scrapy爬虫具体案例详细分析 scrapy,它是一个整合了的爬虫框架, 有着非常健全的管理系统. 而且它也是分布式爬虫, 它的管理体系非常复杂. 但是特别高效.用途广泛,主要用于数据挖掘.检测以及 ...