【lintcode】二分法总结 II
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.
nums = [1, 2, 4, 8, 6, 3] return 8 Given nums = [10, 9, 8, 7], return 10public 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的更多相关文章
- Lintcode: Sort Colors II 解题报告
Sort Colors II 原题链接: http://lintcode.com/zh-cn/problem/sort-colors-ii/# Given an array of n objects ...
- Lintcode: Majority Number II 解题报告
Majority Number II 原题链接: http://lintcode.com/en/problem/majority-number-ii/# Given an array of integ ...
- [LintCode] Wiggle Sort II 扭动排序之二
Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]... ...
- [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 ...
- [LintCode] House Robber II 打家劫舍之二
After robbing those houses on that street, the thief has found himself a new place for his thievery ...
- lintcode:背包问题II
背包问题II 给出n个物品的体积A[i]和其价值V[i],将他们装入一个大小为m的背包,最多能装入的总价值有多大? 注意事项 A[i], V[i], n, m均为整数.你不能将物品进行切分.你所挑选的 ...
- lintcode:排颜色 II
排颜色 II 给定一个有n个对象(包括k种不同的颜色,并按照1到k进行编号)的数组,将对象进行分类使相同颜色的对象相邻,并按照1,2,...k的顺序进行排序. 样例 给出colors=[3, 2, 2 ...
- lintcode: 跳跃游戏 II
跳跃游戏 II 给出一个非负整数数组,你最初定位在数组的第一个位置. 数组中的每个元素代表你在那个位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 样例 给出数组A = ...
- Lintcode: k Sum II
Given n unique integers, number k (1<=k<=n) and target. Find all possible k integers where the ...
随机推荐
- Windows Server 2012安装IIS 8.0
一.安装 1.鼠标右键[This PC]→[Manage] 2.选择[Add Roles and Features] 3.勾选[.Net Framewore 3.5] 和 [.Net Framewor ...
- Spring Boot 指定某个依赖的版本
Spring Boot 是个很好的框架,他为了他的一些功能生效,定义了一些依赖的版本. 比如说:Spring Boot 1.5.x 中elasticSearch是2.4.x的,这个是他本身就定义好的. ...
- IP通信第四周作业
一.选择交换机的主要技能指标是什么? a.背板带宽.二/三层交换吞吐率. b.VLAN类型和数量. c.交换机端口数量及类型. d.支持网络管理的协议和方法.需要交换机提供更加方便和集中式的管理. e ...
- jdbc笔记(二) 使用PreparedStatement对单表的CRUD操作
首先声明,本文只给出代码,并不是做教程用,如有不便之处,还请各位见谅. PreparedStatement相较于Statement,概括来说,共有三个优势: 1. 代码的可读性和易维护性:Prepar ...
- telnet客户端操作memcached增删改查
一,通过telnet连接进入memcached(telnet 本地ip/服务器ip 端口) 进入后回车看效果: 二, 添加数据和取出数据 添加命令: add key名 0(固定) ...
- flask、tornado、BaseHTTPServer性能简单对比
最近写了一个web应用,分别用flask.tornado.BaseHTTPServer都实现了一次,顺便就对比了一下三者的性能,本结果仅对本次测试负责(这句话很内涵,值得推广). 测试工具用了ab,时 ...
- [c/c++] programming之路(28)、结构体存储和内存对齐+枚举类型+typedef+深拷贝和浅拷贝
一.结构体存储 #include<stdio.h> #include<stdlib.h> struct info{ char c; //1 2 4 8 double num; ...
- topcoder srm 640 div1
problem1 link 首先使用两个端点颜色不同的边进行连通.答案是$n-1-m$.其中$m$是联通分量的个数. problem2 link 首先构造一个最小割的模型.左边的$n_{1}$个点与源 ...
- Domain logic approachs
1.transaction script(事务脚本) 概述: 很多企业应用可以看成一系列的事务,每一个事务可以通过使用一个Transaction Script来处理. 用法: 使用Transactio ...
- 0 vs null
看图说话 0 如图所示: 0 表示有纸(值), 但是纸(值)是0. 所以取纸(值)的时可以取, 但是没法用. null 如图所示: null 表示没有纸(值), 是真的啥都没有, 现在你抽纸的时候会出 ...