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: ...
随机推荐
- Linux 的shell 字符串截取很有用。有八种方法。
一 Linux 的字符串截取很有用.有八种方法. 假设有变量 var=http://www.linuxidc.com/123.htm 1 # 号截取,删除左边字符,保留右边字符. echo ${va ...
- devstack重启后不能运行
devstack 重启后没有运行服务. 解释: “Note if you reboot your machine running devstack, you need to rerun stack.s ...
- Android Debugging
Debugging methods for Android Contents [hide] 1 Debuggers 1.1 Kernel and User co-debug with GDB on ...
- HTML5 manifest离线缓存
一.基本概念 离线缓存是HTML5新引入的技术,能够让你的Web应用程序指定哪些文件可以缓存在本地,使得你的网络断开时依然可以通过本地的缓存来进行访问浏览. 二.使用方法 1. MIME type 声 ...
- Java数据类型中String、Integer、int相互间的转换
1.Integer转换成int的方法 Integer i; int k = i.intValue(); 即Integer.intValue(); 2.int转换成Integer int i; Int ...
- Find them, Catch them(poj 1703)
题目大意: 在这个城市里有两个黑帮团伙,现在给出N个人,问任意两个人他们是否在同一个团伙输入D x y代表x于y不在一个团伙里输入A x y要输出x与y是否在同一团伙或者不确定他们在同一个团伙里 思路 ...
- Power Strings(poj 2406)
题意:求字符串中循环节出现的次数 KMP!!! #include<cstdio> #include<iostream> #include<cstring> #def ...
- 如何关闭ie9烦人的提示信息?
①Q:如何关闭“IE 限制活动内容”的提示? A:去掉“IE 限制活动内容”的提示: 1. 找到IE的“Internet选项”: 2. 选择“高级”选项卡: 3. 在“设置”下,找到子标题“安全”,将 ...
- Android之记住密码与自动登陆实现
本文主要讲述了利用sharedpreference实现记住密码与自动登陆功能 根据checkbox的状态存储用户名与密码 将结果保存在自定义的application中,成为全局变量 布局文件 < ...
- 用php输入表格内容
<body> <table width="100%" border="1" cellpadding="0" cellspa ...