Question:

Given a sorted array of strings which is interspersed with empty strings, write a method to find the location of a given string. 
 package POJ;

 public class Main {

     /**
*
* 11.5 Given a sorted array of strings which is interspersed with empty
* strings, write a method to find the location of a given string.
*
*/
public static void main(String[] args) {
Main so = new Main();
} public int search(String[] strs, String str) {
if (strs == null || str == null || str.equals("")) {
return -1;
}
return searchR(strs, str, 0, str.length() - 1);
} private int searchR(String[] strs, String str, int first, int last) {
// TODO Auto-generated method stub
if (last < first)
return -1;
int mid = (first + last) / 2;
if (strs[mid].isEmpty()) {
int left = mid - 1;
int right = mid + 1;
while (true) {
if (left < first && right > last)
return -1;
else if (right <= last && !strs[right].isEmpty()) {
mid = right;
break;
} else if (left >= first && !strs[left].isEmpty()) {
mid = left;
break;
}
right++;
left--;
}
}
if (str.equals(strs[mid]))
return mid;
else if (strs[mid].compareTo(str) < 0)
return searchR(strs, str, mid + 1, last);
else
return searchR(strs, str, first, mid - 1);
} }

CC150 - 11.5的更多相关文章

  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.3

    Question: Given a sorted array of n integers that has been rotated an unknown number of times, write ...

  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. 中位数与第K小元素

    算法实际上是模仿快速排序算法设计出来的,其基本思想也是对输入数组进行递归划分,与快速排序不同的是,它只对划分出来的子数组之一进行递归处理: int randompartition(int a[],in ...

  2. tcp粘包问题(封包)

    tcp粘包分析     http://blog.csdn.net/zhangxinrun/article/details/6721495 解决TCP网络传输“粘包”问题(经典)       http: ...

  3. Python OpenSource Project

    http://www.oschina.net/project/lang/25/python

  4. 73 [面试题]交换一个整数的二进制表示的奇偶位(swapOddEvenBits)

    [本文链接] http://www.cnblogs.com/hellogiser/p/swap-odd-even-bits.html [分析] 假定一个数字是8位数,设为ABCDEFGH ABCDEF ...

  5. POJ 3977

    Subset Time Limit: 30000MS   Memory Limit: 65536K Total Submissions: 1373   Accepted: 228 Descriptio ...

  6. css样式,层叠顺序属性z-index

    在做项目的时候,居然单击后显示的顺序一直被别的li标签压着,最后终于找到了,是css的z-index属性赋值了,值越大,显示的层就越高 详情推荐百度百科:z-index z-index是针对网页显示中 ...

  7. Mac OS Ruby安装 使用RVM

    访问http://www.ruby-lang.org/en/downloads/ 使用第三方工具安装ruby,经过了解,在mac下可以使用macports和rvm安装ruby. 经过实际操作觉得rvm ...

  8. WMI

    https://wiki.jenkins-ci.org/display/JENKINS/Windows+slaves+fail+to+start+via+DCOM#Windowsslavesfailt ...

  9. Man简单介绍

    转自:http://os.51cto.com/art/201312/425525.htm Linux系统提供了相对比较丰富的帮助手册(man),man是manual的缩写,在日常linux系统管理中经 ...

  10. C语言输出格式总结

    转自:http://www.cnblogs.com/scbzljstudy/archive/2011/02/28/1966887.html 1 一般格式    printf(格式控制,输出表列)    ...