CC150 - 11.3
Question:
Given a sorted array of n integers that has been rotated an unknown number of times, write code to find an element in the array. You may assume that the array was originally sorted in increasing order.
package POJ; import java.util.Arrays;
import java.util.Comparator;
import java.util.Hashtable;
import java.util.LinkedList;
import java.util.List; public class Main { /**
*
* 11.3 Given a sorted array of n integers that has been rotated an unknown number of times, write code to find an
* element in the array. You may assume that the array was originally sorted in increasing order.
*
*/
public static void main(String[] args) {
Main so = new Main();
int[] list = { 10, 15, 20, 0, 5 };
System.out.println(so.search(list, 0, 4, 5));
} public int search(int[] list, int left, int right, int x) {
int mid = (right + left) / 2;
if (x == list[mid])
return mid;
if (left > right)
return -1;
if (list[left] < list[mid]) {
// left is normally ordered
if (x >= list[left] && x <= list[mid]) {
return search(list, left, mid - 1, x);
} else {
return search(list, mid + 1, right, x);
}
} else if (list[left] > list[mid]) {
// right is normally ordered
if (x >= list[mid] && x <= list[right]) {
return search(list, mid + 1, right, x);
} else {
return search(list, left, mid - 1, x);
}
} else {
// list[left]==list[mid]
// left half is all repeats
if (list[mid] != list[right]) {
return search(list, mid + 1, right, x);
} else {
// search both halves
int result = search(list, left, mid - 1, x);
if (result == -1)
return search(list, mid + 1, right, x);
else
return result;
}
}
}
}
CC150 - 11.3的更多相关文章
- CC150 - 11.6
Question: Given an M x N matrix in which each row and each column is sorted in ascending order, writ ...
- CC150 - 11.5
Question: Given a sorted array of strings which is interspersed with empty strings, write a method t ...
- CC150 - 11.2
Question: Write a method to sort an array of strings so that all the anagrams are next to each other ...
- CC150 - 11.1
Question: You are given two sorted arrays, A and B, where A has a large enough buffer at the end to ...
- 地区sql
/*Navicat MySQL Data Transfer Source Server : localhostSource Server Version : 50136Source Host : lo ...
- 11.8---维护x的秩(CC150)
思路:比较easy.就是借助hashset让他有序然后就能够比较节省时间了. 答案: public static int[] getRankOfNumber(int[] a, int n){ int[ ...
- 11.7---叠罗汉表演节目(CC150)
1,牛客网第一题:这其实跟找最长递增子序列是一个东西.注意的地方是,返回的是最大的dp,而不是dp[N-1]. 答案: public static int getHeight(int[] men, i ...
- 11.6---矩阵查找元素(CC150)
思路,一旦提到查找就要想到二分查找. public static int[] findElement(int[][] a, int n, int m, int key) { // write code ...
- 11.5---含有空字符串的字符串查找(CC150)
注意,1,"" 和 " ".是不同的,空字符串指的是"": 2,注意String的compareTo.小于是指<0.并不是==-1: ...
随机推荐
- js中document.documentElement 和document.body 以及其属性 clientWidth等
在设计页面时可能经常会用到固定层的位置,这就需要获取一些html对象的坐标以更灵活的设置目标层的坐标,这里可能就会用到document .body.scrollTop等属性,但是此属性在xhtml标准 ...
- postgresql 锁的定位
今天碰到了一个问题,锁定穷根追底把postgresql的锁研究了一番. 数据库查看锁 可以通过表 pg_locks来查看有哪些锁.sql如下: select a.locktype,a.database ...
- ubuntu14.04中国源
deb http://cn.archive.ubuntu.com/ubuntu/ trusty main restricted universe multiverse deb http://cn.ar ...
- OAuth
http://oauth.net http://oauth.net/2/ http://tools.ietf.org/html/rfc6749 人人网:http://wiki.dev.renren.c ...
- MySQL数据库备份和还原的常用命令
其实很多情况下mysql备份就是采用了这些命令,例如: mysql导入和导出数据 linux自动定时备份web程序和mysql数据库 备份MySQL数据库的命令 mysqldump -hhostnam ...
- delphi 换行操作 Word
delphi 换行操作 我将我的商用<旅行社管理系统>的 发团通知 部分奉献给您,望对您有所帮助. procedure TFrmMain.N327Click(Sender: TObject ...
- ShortestPath:Layout(POJ 3169)(差分约束的应用)
布局 题目大意:有N头牛,编号1-N,按编号排成一排准备吃东西,有些牛的关系比较好,所以希望他们不超过一定的距离,也有一些牛的关系很不好,所以希望彼此之间要满足某个关系,牛可以 ...
- ip
D组播地址 主机号 用于识别该网络中的主机. IP地址分为五类,A类保留给政府机构,B类分配给中等规模的公司,C类分配给任何需要的人,D类用于组播,E类用于实验,各类可容纳的地址数目不同. A.B.C ...
- Myeclipse中把java代码导成UML类图
Myeclipse中把java代码导成UML类图 1.右键点击项目名称,选择New-------àUML2 Model 2.给类图命名 3.导成类图 1)如果要把整个项目导成类图,则把整个项目拖到类图 ...
- git merge 合并分支
git merge 用来做分支合并,将其他分支中的内容合并到当前分支中.比如分支结构如下: master / C0 ---- C1 ---- C2 ---- C4 \ C3 ---- C5 \ iss ...