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: ...
随机推荐
- Linux zabbix 配置注意事项
发现php-fpm启动之后,找不到 php-fpm.pid文件??? vi php-fpm.conf 去掉里面那个 pid = run/php-fpm.pid 前面的分号然后再启动php-fpm才能自 ...
- Android自定义dialogdemo
很多时候,我们需要自己去定义dialog,目前我们就遇见了这样一个需求,我的想法是自己定义一个dialog,如果有list的话就使用listview,如果有msg的话就使用msg,并且取消和确定按钮也 ...
- Solr5.3.1通过copyField设置多个field(字段)同时检索
如果业务需要我们对多个field同时进行检索,有没有什么好的办法呢?非常幸运的是Solr为我们提供了copyField对多个field进行索引和检索.然而配置也非常简单. 修改schame.xml,添 ...
- hdu 1213 How Many Tables 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 有关系(直接或间接均可)的人就坐在一张桌子,我们要统计的是最少需要的桌子数. 并查集的入门题,什 ...
- 【python】zip()函数
来源:http://www.cnblogs.com/frydsh/archive/2012/07/10/2585370.html zip函数接受任意多个(包括0个和1个)序列作为参数,返回一个tupl ...
- [Android Memory] android 警告:Exported activity does not require permission
在一个应用程序中添加了多个antivity后,在manifest.xml文件中会除了主Activity外,其它的Activity属性中都会有个警告: Exported activity does no ...
- solr6.0学习
solr6.0学习(一)环境搭建准备工作:目前最新版本6.0.下载solr 6.0:Solr6.0下载JDK8 下载jdk1.8:jdk1.8[solr6.0是基于jdk8开发的]tomcat8.0 ...
- mysql_1
1.mysql> select NOW();等效于select user()\g+---------------------+| NOW() |+---------- ...
- Android之shape属性详解
有时候 ,为了满足一些需求,我们要用到 shape 去定义 一些背景,shape 的用法 跟图片一样 ,可以给View设置 Android:background="@drawable/sha ...
- .NET的堆和栈01,基本概念、值类型内存分配
当我们对.NET Framework的一些基本面了解之后,实际上,还是很有必要了解一些更底层的知识.比如.NET Framework是如何进行内存管理的,是如何垃圾回收的......这样,我们才能写出 ...