【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 ...
随机推荐
- RDLC报表数据集的一个细节,导致错误为 尚未数据源提供数据源实例
报表中,数据集的名字DataSet_CZ, 这里报表这样加载,视乎是的. reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporti ...
- Elastic-Job 配置介绍
作业配置 与Spring容器配合使用作业,可以将作业Bean配置为Spring Bean,可在作业中通过依赖注入使用Spring容器管理的数据源等对象.可用placeholder占位符从属性文件中取值 ...
- java 几个实用的小工具
1.除法运算 编程的人都知道,java中的“/”.“%”运算,其中前者为取整,后者取余数.那么有没有快捷的运算方法取正常的运算结果呢? 查了资料,发现很简单.代码如下: public static S ...
- ogg同步DDL时,源和目标端表空间名称不同的解决思路
在OGG同步过程中,经常会碰上有创建表或表空间的同步,往往因为源和目标的平台不同,如aix->linux or linux->windows,这两个平台的表空间也经常不同,在目标端执行DD ...
- 使用session和cookie实现用户登录:一个登录页面,一个servlet,一个登录成功页面
文件目录 1.登录页面 <%@ page language="java" contentType="text/html; charset=utf-8" p ...
- Exp2 后门原理与实践 20164303 景圣
Exp2 后门原理与实践 一.基础问题回答: 1.例举你能想到的一个后门进入到你系统中的可能方式? 答:在网上点击不安全的网页或链接. 2.例举你知道的后门如何启动起来(win及linux)的方式? ...
- 网站搭建 (第06天) Ckeditor编辑器
一.前言 富文本编辑器,Rich Text Editor, 简称 RTE, 是一种可内嵌于浏览器,所见即所得的文本编辑器,这是百度百科的对富文本编辑器的解释.我们可以借助富文本编辑器,编辑出来一个包含 ...
- 理解bootstrap的列偏移offset 和 推拉push/pull的区别?
参考: http://www.cnblogs.com/jnslove/p/5430481.html & https://blog.csdn.net/hly_coder/article/deta ...
- 牛客OI周赛9-提高组题目记录
牛客OI周赛9-提高组题目记录 昨天晚上做了这一套比赛,觉得题目质量挺高,而且有一些非常有趣而且非常清奇的脑回路在里边,于是记录在此. T1: 扫雷 题目链接 设 \(f_i\) 表示扫到第 \(i\ ...
- Vue-admin工作整理(二):项目结构个人配置
通过上一篇文章(Vue-admin工作整理(一):项目搭建)操作完毕后,基础项目已经搭建,下面就要对项目本身进行一下项目结构调整来符合自己的项目要求 1.首先要对package.json文件进行调整, ...