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.
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的更多相关文章
- 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.3
Question: Given a sorted array of n integers that has been rotated an unknown number of times, write ...
- 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: ...
随机推荐
- DELPHI设置枚举类型size
delphi枚举类型长度默认为2个字节(单字),而在C中枚举为4个字节(双字),如果需要跨这两个平台编程,传输结构时会由于数据长度不一造成灾难. 经过查找资料,原来delphi可以通过{$Z+} {$ ...
- ubuntu 桌面版性能调优
http://www.howtogeek.com/115797/6-ways-to-speed-up-ubuntu/
- FFT(1)
FFT Complex struct complex{ double re,im; complex(double r,double i){re=r,im=i;} complex(){re=0.0,im ...
- vim实用技巧
<1> 删除空格: :% s/ //gi #正则为一个空格,替换为空,全局匹配. <2> 删除空行: :g /^\n*$/ d #g为global 正则为:行开始+换 ...
- nested exception is org.xml.sax.SAXParseException; lineNumber: 8; columnNumber: 56; cvc-complex-type.2.4.c通配符的匹配很全面, 但无法找到元素 'dubbo:application' 的声明
严重: Exception sending context initialized event to listener instance of class org.springframework.we ...
- AJAX 异步交互基本总结
AJAX (Asynchronous JavaScript and Xml) 直译中文 - javascript和XML的异步 同步与异步的区别: 同步交互 执行速度相对比较慢 响应的是完整的HTML ...
- javascript的onbeforeunload函数在IOS上运行
今天在做项目的时候,组长让我用iPad测试一下前面写的离线缓存,后退不刷新页面,发现在iPad上onbeforeunload函数在iPad上一带而过,不运行??? 无奈之下,发现原来在IOS上,有自己 ...
- 【Qt】学习笔记(一)
1.setupUi(this) : setupUi(this)是由.ui文件生成的类的构造函数这个函数的作用是对界面进行初始化它按照我们在Qt设计器里设计的样子把窗体画出来 setupUi(this) ...
- HDU 3835 R(N)
R(N) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- 分页管理的AJAX实现
bookMgr.jsp <%-- Document : bookMgr.jsp Created on : 2016-11-7, 9:48:21 Author : guanghe --%> ...