注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
网上找了一下DrawBridge的OA做了一下 把题目和贴出来分享
如果有错误的地方请大家指正 谢谢
import java.util.*;
public class DrawBridgeOA {
public static void main(String[] args) {
Printer.p(mySort(new int[]{1,Integer.MIN_VALUE,0}));
}
/*
* Sort an array by number of 1s in the binary of integer, if number of 1s are same,
* the smaller one should be placed first.
*
* Example
*
* input [1,2,3,4,5]
* return [1,2,4,3,5]
*
* */
public static int[] mySort(int[] nums) {
int[] res = new int[nums.length];
PriorityQueue<Integer> pq = new PriorityQueue<>(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
int count1 = countOnes((int)o1);
int count2 = countOnes((int)o2);
if (count1 == count2) {
return Integer.compare(o1, o2);
// in case of integer overflow
}
return count1 - count2;
}
});
for (int num : nums) {
pq.offer(num);
}
for (int i = 0; i < res.length; i++) {
res = pq.poll();
}
return res;
}
public static int countOnes(int num) {
int count = 0;
while (num != 0) {
count += num & 1;
num >>>= 1;
}
return count;
}
/*
* Given a positive integer target, count all the combinations of contiguous positive
* integers that sum up to the target.
*
* For Example,
* target = 15
* return 3
*
66d">jungleBook(int[] nums) {
int max = 0;
for (int i = 0; i < nums.length; i++ ){
max = Math.max(max, cal(nums, i));
}
return max;
}
public static int cal (int[] nums, int index) {
int predator = nums[index];
if (predator == -1) {
jungleMap.put(index, 1);
return 1;
}
int val;
if (jungleMap.containsKey(predator)) {
val = jungleMap.get(predator)+1;
} else {
val = cal(nums, predator)+1;
}
jungleMap.put(index, val);
return val;
}
/*
* Given a set of inclusive intervals
* Calculate the minimum size of a set of number
* where each interval contains at least two numbers in this set.
*
* Example
* given [1,3], [1,4], [2,5].
* return 2. ([2,3])
*
* given [2,4], [3,6], [0,2], [4,7].
* return 4. ([0,2,4,6] or [1,2,4,5])
* */
}
|