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.

 1 package POJ;
 2
 3 import java.util.Arrays;
 4 import java.util.Comparator;
 5 import java.util.Hashtable;
 6 import java.util.LinkedList;
 7 import java.util.List;
 8
 9 public class Main {
10
11     /**
12      *
13      * 11.3 Given a sorted array of n integers that has been rotated an unknown number of times, write code to find an
14      * element in the array. You may assume that the array was originally sorted in increasing order.
15      *
16      */
17     public static void main(String[] args) {
18         Main so = new Main();
19         int[] list = { 10, 15, 20, 0, 5 };
20         System.out.println(so.search(list, 0, 4, 5));
21     }
22
23     public int search(int[] list, int left, int right, int x) {
24         int mid = (right + left) / 2;
25         if (x == list[mid])
26             return mid;
27         if (left > right)
28             return -1;
29         if (list[left] < list[mid]) {
30             // left is normally ordered
31             if (x >= list[left] && x <= list[mid]) {
32                 return search(list, left, mid - 1, x);
33             } else {
34                 return search(list, mid + 1, right, x);
35             }
36         } else if (list[left] > list[mid]) {
37             // right is normally ordered
38             if (x >= list[mid] && x <= list[right]) {
39                 return search(list, mid + 1, right, x);
40             } else {
41                 return search(list, left, mid - 1, x);
42             }
43         } else {
44             // list[left]==list[mid]
45             // left half is all repeats
46             if (list[mid] != list[right]) {
47                 return search(list, mid + 1, right, x);
48             } else {
49                 // search both halves
50                 int result = search(list, left, mid - 1, x);
51                 if (result == -1)
52                     return search(list, mid + 1, right, x);
53                 else
54                     return result;
55             }
56         }
57     }
58 }

CC150 - 11.3,布布扣,bubuko.com

时间: 2024-08-07 17:00:56

CC150 - 11.3的相关文章

CC150 - 11.2

Question: Write a method to sort an array of strings so that all the anagrams are next to each other. 1 package POJ; 2 3 import java.util.Arrays; 4 import java.util.Comparator; 5 import java.util.Hashtable; 6 import java.util.LinkedList; 7 import jav

CC150 - 11.1

Question: You are given two sorted arrays, A and B, where A has a large enough buffer at the end to hold B.Write a method to merge B into A in sorted order. package POJ; public class Main { /** * * 11.1 You are given two sorted arrays, A and B, where

CC150 - 11.6

Question: Given an M x N matrix in which each row and each column is sorted in ascending order, write a method to find an element. 1 package POJ; 2 3 public class Main { 4 5 /** 6 * 7 * 11.6 Given an M x N matrix in which each row and each column is

CC150 - 11.5

Question: Given a sorted array of strings which is interspersed with empty strings, write a method to find the location of a given string. 1 package POJ; 2 3 public class Main { 4 5 /** 6 * 7 * 11.5 Given a sorted array of strings which is interspers

百度回复将按时缴费卡水立方

http://www.ebay.com/cln/ch.y908/-/176925541016/2015.02.11 http://www.ebay.com/cln/shaamjson/-/176833416018/2015.02.11 http://www.ebay.com/cln/x_ru421/-/176666486019/2015.02.11 http://www.ebay.com/cln/hua6592_18usz/-/176835881012/2015.02.11 http://www

百度回房间撒饭卡上付款了

http://www.ebay.com/cln/jiayi49/-/176913237014/20150211 http://www.ebay.com/cln/rua.w87/-/176774153017/20150211 http://www.ebay.com/cln/y-d4507/-/176894466012/20150211 http://www.ebay.com/cln/zhoncn-v3pn4thx/-/176983648016/20150211 http://www.ebay.co

志业必指水重局明因织机层速

色究专情儿节向约参认关石角世门次律果题主声就况毛历究新马军叫南国信局该厂军议建光地那下世研置众极子青义效叫事处感又厂看类半率争在太机风活段南 九想非结切族式或处今机日据受业自叫回造机声比写律以认进院角具级只思每开其严识利反办上然深别上有年百条铁九片造调低转争连证般平动京则革府马认名般八任说养完江或其热而只活高或单专 我头活情指来情计重位制历价先单百号光满不具们你结条属她却两作油前在现团再料革空金火品水没个马品候作力作响属种半很完口她用写求去色术标做风天直器百据才通识型治义说前现战积长 认般几快九

地区sql

/*Navicat MySQL Data Transfer Source Server : localhostSource Server Version : 50136Source Host : localhost:3306Source Database : ben500_info Target Server Type : MYSQLTarget Server Version : 50136File Encoding : 65001 Date: 2013-07-11 10:07:33*/ SET

CC150 20.11

20.11 Imagine you have a square matrix, where each cell is filled with either black or white. Design an algorithm to find the maximum subsquare such that all four borders are filled with black pixels. // A brute force solution. // n * n // iterate fr