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. apache virtualhost配置 apache配置多个网站

    第一步 apache下httpd.conf文件 启用模块LoadModule vhost_alias_module modules/mod_vhost_alias.so 第二步 apache下http ...

  2. Kruskal vs Borůvka

    做了个对比.Borůvka算法对于稠密图效果特别好.这两个都是求生成森林的算法.Prim+heap+tarjan过于难写不写了. V=200,E=1000 Kruskal method 4875048 ...

  3. 二模Day2题解

    小明搬家 题目描述 小明要搬家了,大家都来帮忙. 小明现在住在第N楼,总共K个人要把X个大箱子搬上N楼. 最开始X个箱子都在1楼,但是经过一段混乱的搬运已经乱掉了.最后大家发现这样混乱地搬运过程效率太 ...

  4. 【Network】一张图看懂 Reactor 与 Proactor 模型的区别

    首先来看看Reactor模式,Reactor模式应用于同步I/O的场景.我们以读操作为例来看看Reactor中的具体步骤:读取操作:1. 应用程序注册读就需事件和相关联的事件处理器2. 事件分离器等待 ...

  5. 【Spring】Spring系列7之Spring整合MVC框架

    7.Spring整合MVC框架 7.1.web环境中使用Spring 7.2.整合MVC框架 目标:使用Spring管理MVC的Action.Controller 最佳实践参考:http://www. ...

  6. .html和.htm的区别

    很多人会认为网页扩展名html和htm是等同的,但事实上他们还是有区别的. 包含HTML内容的文件最常用的扩展名是.html,但是像DOS这样的旧操作系统限制扩展名为最多3个字符,所以.htm扩展名也 ...

  7. python在线文档

    中文 http://python.usyiyi.cn/--------------------------不完整 http://www.pythondoc.com/pythontutorial27/i ...

  8. cocos2d-x 制作系统公告

    2013-12-15 21:57:33 下载地址:http://download.csdn.net/detail/jackyvincefu/6434549 (摘自:CSDN资源) CTestLayer ...

  9. sqlcmd

    使用sqlcmd可以在批处理脚本中执行SQL.虽然这个命令的参数很多,但幸运的是,我们不需要全部理解,在这里简要介绍以下几个: { -U login_id [ -P password ] } | –E ...

  10. Java for LeetCode 061 Rotate List

    Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given ...