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: ...
随机推荐
- RPM常用组合【转载】
RPM常用组合 -ivh:安装显示安装进度--install--verbose--hash -Uvh:升级软件包--Update: -qpl:列出RPM软件包内的文件信息[Query Package ...
- sed替换字符串时,使用正则表达式的注意事项
sed的使用方法为: 使用单个模式替换:sed 's/pattern/replacement/flags' filename,例如echo 'abc' | sed 's/a/A/'-->Abc ...
- Longest Substring with At Most K Distinct Characters
Given a string, find the longest substring that contains only two unique characters. For example, gi ...
- BASH相关
颜色 http://www.cnblogs.com/lr-ting/archive/2013/02/28/2936792.html http://segmentfault.com/q/10100000 ...
- springJDBC一对多关系,以及Java递归,jsp递归的实现
maven编译,springMVC+spring+springJDBC框架. 要实现的功能是一个文件夹下,可能显示n个文件夹,每个文件夹下又可能显示n个文件夹.... 前台效果:
- Linux常用命令总结--分布式应用部署与监控
1 kill所有相关进程ps -ef | grep -i 进程名 | grep -v "grep" | awk '{print $2}' |xargs kill 2 查询当前用户占 ...
- 理解LoadRunner中的局部变量和全局变量
在C语言中,变量有效性的范围称变量的作用域.不仅对于形参变量,C语言中所有的量都有自己的作用域,按作用域范围不同,可分为局部变量和全局变量两种变量类型. 在LoadRunner中应用全局变量和局部变量 ...
- 如何在 Laravel 中使用 SMTP 发送邮件(适用于 163、QQ、Gmail 等)
Laravel 和 Laravel 的邮件发送使用方式完全一致.Laravel 的邮件发送中文文档在:http: 邮箱为例,展示如何用 Laravel 内置的邮件发送类来发送邮件. 配置 修改邮 ...
- Android快捷键
Android快捷键ALT+/ :在布局文件中,提示输入的内容Shift + Ctrl + / :注释Shift + Ctrl + \ :解除注释
- 【Tyvj】1473校门外的树3 线段树/树状数组 <区间修改+单点访问>
描述 校门外有很多树,有苹果树,香蕉树,有会扔石头的,有可以吃掉补充体力的……如今学校决定在某个时刻在某一段种上一种树,保证任一时刻不会出现两段相同种类的树,现有两个操作:K=1,读入l,r表示在l ...