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的更多相关文章

  1. CC150 - 11.6

    Question: Given an M x N matrix in which each row and each column is sorted in ascending order, writ ...

  2. CC150 - 11.5

    Question: Given a sorted array of strings which is interspersed with empty strings, write a method t ...

  3. CC150 - 11.2

    Question: Write a method to sort an array of strings so that all the anagrams are next to each other ...

  4. CC150 - 11.1

    Question: You are given two sorted arrays, A and B, where A has a large enough buffer at the end to ...

  5. 地区sql

    /*Navicat MySQL Data Transfer Source Server : localhostSource Server Version : 50136Source Host : lo ...

  6. 11.8---维护x的秩(CC150)

    思路:比较easy.就是借助hashset让他有序然后就能够比较节省时间了. 答案: public static int[] getRankOfNumber(int[] a, int n){ int[ ...

  7. 11.7---叠罗汉表演节目(CC150)

    1,牛客网第一题:这其实跟找最长递增子序列是一个东西.注意的地方是,返回的是最大的dp,而不是dp[N-1]. 答案: public static int getHeight(int[] men, i ...

  8. 11.6---矩阵查找元素(CC150)

    思路,一旦提到查找就要想到二分查找. public static int[] findElement(int[][] a, int n, int m, int key) { // write code ...

  9. 11.5---含有空字符串的字符串查找(CC150)

    注意,1,"" 和 " ".是不同的,空字符串指的是"": 2,注意String的compareTo.小于是指<0.并不是==-1: ...

随机推荐

  1. HDU 3371 kruscal/prim求最小生成树 Connect the Cities 大坑大坑

    这个时间短 700多s #include<stdio.h> #include<string.h> #include<iostream> #include<al ...

  2. django admin 扩展

    添加自定义动作: 例子,添加一个方法,批量更新文章,代码如下: from django.contrib import admin from myapp.models import Article de ...

  3. 查看现有运行的linux服务器有多少内存条

    i161 admin # ssh 192.168.5.209 dmidecode | grep 'Ending Address' -B1 -A2    Starting Address: 0x0000 ...

  4. Sublime Text 2 入门及技巧

    看了 Nettuts+ 对 Sublime Text 2 的介绍, 立刻就兴奋了,诚如作者 Jeffrey Way 所说:“<永远的毁灭公爵>都发布了,TextMate 2 还没发”,你还 ...

  5. Java锁之自旋锁详解

    锁作为并发共享数据,保证一致性的工具,在JAVA平台有多种实现(如 synchronized 和 ReentrantLock等等 ) .这些已经写好提供的锁为我们开发提供了便利,但是锁的具体性质以及类 ...

  6. 手动构建Servlet项目的流程

    前面讨论过手动建立jsp的项目,jsp是tomcat服务器负责编译执行,所以配置相对简单,而Servlet需要先把java源文件编译成字节码class文件,然后再执行,所以需要servlet-api. ...

  7. Android开发数据库三层应用-DataSnap

    Android开发数据库三层应用-DataSnap http://www.2ccc.com/news/Html/?1517.html 核心提示:我觉得Delphi最强大的的功能之一就是开发数据库三层应 ...

  8. iOS开发——开发实战篇&版本控制SVN和Git使用详解

     版本控制SVN和Git使用详解     公司的实际开发中,在天朝使用较多的还是SVN,因为SVN是集中式的,在天朝上班你们都懂的!     -----------------svn--------- ...

  9. sublime text 3 使用过程总结记录

    自定义的设置: "save_on_focus_lost": true //在文件失去焦点的时候自动保存

  10. vector.end() 指向的节点

    存储器vector, vector.end() 指向的是最后的结束符,而不是最后一个元素.