【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 ...
随机推荐
- flutter-fluro
路由传参 route.dart import 'package:fluro/fluro.dart'; //添加页面 import 'package:m/pages/loginPage.dart'; i ...
- BZOJ5279: [Usaco2018 Open]Disruption
题目大意:给你一棵n个节点的树,这n条边称为原边,另给出m条带权值的额外边,求删去每条原边后通过给出的m额外条边变回一棵树的最小价值.题解:看完题面以为是Tarjan连通性之类的题目,冷静分析后想到是 ...
- dataguard从库移动数据文件
------------方法1从库移动数据文件路径方法1--------------将表空间offline的方法不行 1.退出日志应用alter database recover managed st ...
- objectarx 把当前图形输出
方法1: AcDbDatabase *pdb; acdbCurDwg()->wblock(pdb); pdb->saveAs(str); pdb->closeInput(true); ...
- Best Practices for Assembly Loading
原文链接 This article discusses ways to avoid problems of type identity that can lead to InvalidCastExce ...
- 2019 年 GrapeCity Documents 产品路线图
前言 | 问题背景 随着软件行业引入新的硬件和操作系统,我们看到更多的托管框架与.NET技术保持同步.Microsoft的.NET Standard和.NET Core定义了一个跨平台规范,为应用程序 ...
- Python中不尽如人意的断言Assertion
Python Assert 为何不尽如人意 Python中的断言用起来非常简单,你可以在assert后面跟上任意判断条件,如果断言失败则会抛出异常. >>> assert 1 + 1 ...
- myeclipse中tomcat配置虚拟路径,用于存储及上传图片
由于双击tomcat后只出现了overview窗口,没有出现Models窗口,如下图所示,故eclipse中的方法不能用. 采用另一种方法 在自己的tomcat的安装目录下 ( 我安装目录的是 D:\ ...
- Luogu P1245 电话号码
Luogu P1245 电话号码 谨以此题解致敬我的初中英语老师孙菡老师,她带的班默写不过的人数总是像电话号码那样屈指可数 玄学问题? 本题的SPJ似乎已经基本没有问题了,只要 文末没有多余的空格和回 ...
- npm版本安装问题
问题一 描述 运行npm install之后,前端页面console控制台报错,invalid props. 排查 1. 排除了代码问题,完全一样的代码,其他人的运行无误. 2.猜想可能是版本号问题, ...