// Given a sorted array that has been transposed (that is, a portion has been removed from one end and attached to the other), write a function to determine if a given number is present in the array.
// For example, here's a transposed array: 6 7 1 2 3 4 5. Note: numbers don't have to be sequential.
// Task #1: Find number "1". Should return true (or index "2").
// Task #2: Find number "4". Should return true (or index "5").
// 1. Find the tranposed pos. return index 2
// 2. do binary search to check the target
/* test case1
begin mid end
6 7 1 2 3 4 5
case 2
6 7 1 2 3 4 5 , Search for 7
case 3
1 1 2 1 1 1 1 1 1 1
*/
public class solution {
public boolean findTarget (int[] array, int target) {
if (array == null || array.length == 0) return false;
int n = array.length;
int begin = 0;
int end = n -1;
while (begin <= end) {
int mid = begin + (end - begin) /2;
int midVal = array[mid];
if (midVal == target) return true;
if (array[begin] <= midVal) {
if ( target < midVal && target >= array[begin]){
end = mid - 1;
} else {
begin = mid + 1;
}
}
if ( midVal <= array[end]) {
if (target > midVal && target <= array[end]) {
begin = mid + 1;
else {
end = mid -1;
}
}
}
return false;
}
}
/* This class will be given a list of words (such as might be tokenized
* from a paragraph of text), and will provide a method that takes two
* words and returns the shortest distance (in words) between those two
* words in the provided text.
* Example:
* WordDistanceFinder finder = new WordDistanceFinder(Arrays.asList("the", "quick", "brown", "fox", "quick"));
* assert(finder.distance("fox","the") == 3);
* assert(finder.distance("quick", "fox") == 1);
*
* "quick" appears twice in the input. There are two possible distance values for "quick" and "fox":
* (3 - 1) = 2 and (4 - 3) = 1.
* Since we have to return the shortest distance between the two words we return 1.
*/
public class WordDistanceFinder {
public String[] wordArr;
public WordDistanceFinder (List<String> words) {
// implementation here
// HashMap<, Integer> map = new HashMap<>(); doesn't work
HashMap<String, List<Integer>> map = new HashMap<>();
// list of words
}
public int distance (String wordOne, String wordTwo) {
// implementation here
int i1 = -1, i2 = -1;
int min = words.length;
for (int i = 0; i < words.length; i++) {
String word = words[i];
if (word.equals(wordOne) ) {
i1= i;
if ( i2 >= 0) min = Math.min(min, i1 - i2);
} else if (word.equals(wordTwo)) {
i2 = i;
if ( i1 >= 0) min = Math.min(min, i2- i1);
}
}
return min;
}
}
2016-03-29-LinkedIn Phone Interview Questions
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- what’s the difference between abstract class and interfac...
- March 17, 2016 Summary and introducation section Spend ti...
- 面试问题 1.在多线程情况下的Singleton写法 使用 volatile 有两个功用:1)这个变量不会在多个线...
- 早上6:38醒了,就在床上写点东西,梳理一下要准备的技术和项目描述。冷不丁有一个电话打进来了。我没听清楚是recr...