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: ...
随机推荐
- 对比WDCP面板与AMH面板的区别与选择
转载: http://www.laozuo.org/2760.html | 老左博客 随着VPS主机的性价比提高(其实就是降价)我们很多站长会越来越多的选择使用VPS搭建网站或者运营一些项目,相比较而 ...
- Java锁之自旋锁详解
锁作为并发共享数据,保证一致性的工具,在JAVA平台有多种实现(如 synchronized 和 ReentrantLock等等 ) .这些已经写好提供的锁为我们开发提供了便利,但是锁的具体性质以及类 ...
- c++标准库中几个常见的数据结构的区别和应用规则
转载自http://www.lifecrunch.biz/archives/202 vector和built-in数组类似,它拥有一段连续的内存空间,并且起始地址不变,因此它能非常好的支持随即存取,即 ...
- Java for LeetCode 023 Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 解 ...
- BestCoder15 1002.Instruction(hdu 5083) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5083 题目意思:如果给出 instruction 就需要输出对应的 16-bit binary cod ...
- fcitx-sogoupinyin下载地址和安装
伴随着Deepin 12.12 beta的发布,搜狗输入法也与我们见面了.在发布前几日Deepiner也通过各种途径向我们展示了搜狗Linux输入法,当然也掉足了胃口. 来自官方的截图: 当然令很多U ...
- August 4th, 2016, Week 32nd, Thursday
How does the world look through your eyes? 你眼中的世界是什么样的呢? This morning I saw a girl that is just the ...
- 强制JSP页面刷新,防止被服务器缓存(可用于静态include强制刷新)
对于jsp页面,为了防止页面被服务器缓存.始终返回同样的结果. 通常的做法是在客户端的url后面加上一个变化的参数,比如加一个当前时间. 我现在使用的方法是在jsp头部添加以下代码: <% ...
- windows 说“我爱你”
CreateObject("SAPI.SpVoice").Speak "I love YOU" 保存vbs
- 取余运算(codevs 1497)
题目描述 Description 输入b,p,k的值,编程计算bp mod k的值.其中的b,p,k*k为长整型数(2^31范围内). 输入描述 Input Description b p k 输出描 ...