j****a 发帖数: 55 | 1 楼上的看起来比较elegant,我随笔写的,不知道对错啊...
// assume A picks first, return difference between what A picks and what B
picks.
int pickNumber(int* array, int begin, int end, HashTable* hashTable) {
int result;
if (getFromHash(hashTable, begin, end, &result)) {
return result;
}
if (end == begin) {
putToHash(hashTable, begin, end, -array[begin]);
return -array[begin];
}
int diff1 = pickNumber(array, begin + 1, end);
int diff2 = pickNumber(array, begin, end +... 阅读全帖 |
|
i**********e 发帖数: 1145 | 2 Worst case is n/2 comparisons.
Can be proven no better algorithm than O(n).
bool isExist(int A[], int n, int target) {
int i = 0, j = n-1;
while (i <= j) {
int left = A[i];
int right = A[j];
int diff1 = abs(target - left);
int diff2 = abs(target - right);
if (diff1 == 0 || diff2 == 0) {
return true;
} else {
i += diff1;
j -= diff2;
}
}
return false;
}
一些常见面试题的答案与总结 -
http://www.ihas1337cod... 阅读全帖 |
|
q**j 发帖数: 10612 | 3 see the modified code. here i assume diff1 is more important than diff2.
proc sql;
create table output as
select a.*, b.*, diff1 = a.US_BQuant - b.CAN_BQuant,
diff2 = a.US_CQuant - b.US_CQuant
from uscenter a, canadacenter b
where a.date = b.date
order by a.US_bloodcenterID, a.Date, diff1, diff2;
quit;
data output;
set output;
by US_bloodcenterID USDate;
if first.USDate;
run; |
|
S*******C 发帖数: 822 | 4 //return the closet number
public double closestNumber(double[] nums, double target) {
if (nums == null || nums.length == 0) {
return -1;
}
int low = 0, high = nums.length - 1;
while (low < high) {
int mid = low + (high - low) / 2;
double diff1 = Math.abs(nums[mid] - target);
double diff2 = Math.abs(nums[mid + 1] - target);
if (diff1 >= diff2) {
low = mid + 1;
} else {
... 阅读全帖 |
|
c*****d 发帖数: 6045 | 5 2. 没看懂这道题问什么呢
Sun ==> Mon ==>Tue ==>Wed ==>Thur ==>Fri ==>Sat
FULL0==>DIFF1==>DIFF2==>FULL1==>DIFF3==>DIFF4==>DIFF5
恢复策略是,
failure before Mon backup, FULL0 + log
failure before Tue backup, FULL0 + DIFF1 + log
failure before Wed backup, FULL0 + DIFF2 + log
failure before Thur backup, FULL1 + log
failure before Fri backup, FULL1 + DIFF3 + log
failure before Sat backup, FULL1 + DIFF4 + log
failure before Sun backup, FULL1 + DIFF5 + log
Sun ==> Mon ==>Tue ==>Wed ==>Thur ==>Fri |
|
c*****d 发帖数: 6045 | 6 2. 没看懂这道题问什么呢
Sun ==> Mon ==>Tue ==>Wed ==>Thur ==>Fri ==>Sat
FULL0==>DIFF1==>DIFF2==>FULL1==>DIFF3==>DIFF4==>DIFF5
恢复策略是,
failure before Mon backup, FULL0 + log
failure before Tue backup, FULL0 + DIFF1 + log
failure before Wed backup, FULL0 + DIFF2 + log
failure before Thur backup, FULL1 + log
failure before Fri backup, FULL1 + DIFF3 + log
failure before Sat backup, FULL1 + DIFF4 + log
failure before Sun backup, FULL1 + DIFF5 + log
Sun ==> Mon ==>Tue ==>Wed ==>Thur ==>Fri |
|
g******0 发帖数: 221 | 7 How does this work?
If I understand the problem correctly, the array can be increasing and
decreasing. How do the following lines work?
i += diff1;
j -= diff2;
If the example is 5 6 7 6 7 8 9, the code doesn't seem to work, does it?
我有可能题目没有看明白,请指点。 |
|
r*******e 发帖数: 7583 | 8 sum of diff != |sum of male - sum of female|
sum of diff = |diff1| + |diff2| ... + |diffn|
height |
|