Microsoft questions - 海外工作
By Sierra Rose
at 2007-12-15T06:47
at 2007-12-15T06:47
Table of Contents
※ 引述《[email protected] (Go cubs!)》之銘言:
: Given 2 sorted arrays, a[] and b[]
: combine them with no extra array(or linear space).
: e.g.: a[10] = [1, 3, 5, 7, 9]
: b[3] = [2, 4, 6]
: the result is a[] = [1, 2, 3, 4, 5, 6, 7, 9], assuming a[] has enough space.
: Can anyone solve it? I don't see a good solution(meaning O(N)) yet.
反過來走如何?
int apos = 4;
int bpos = 2;
for(int i = a.length -1 ; i >= 0 ; i--){
if(apos < 0){
a[i] = b[bpos];
bpos --;
}else if(bpos <0){
a[i] = a[apos];
apos --;
}else if(a[apos] >= b[bpos]){
a[i] = a[apos];
apos --;
} else {
a[i] = b[bpos];
bpos --;
}
}
試過幾個edge case都沒問題,這應該就是O(n)的一個解答了
--
: Given 2 sorted arrays, a[] and b[]
: combine them with no extra array(or linear space).
: e.g.: a[10] = [1, 3, 5, 7, 9]
: b[3] = [2, 4, 6]
: the result is a[] = [1, 2, 3, 4, 5, 6, 7, 9], assuming a[] has enough space.
: Can anyone solve it? I don't see a good solution(meaning O(N)) yet.
反過來走如何?
int apos = 4;
int bpos = 2;
for(int i = a.length -1 ; i >= 0 ; i--){
if(apos < 0){
a[i] = b[bpos];
bpos --;
}else if(bpos <0){
a[i] = a[apos];
apos --;
}else if(a[apos] >= b[bpos]){
a[i] = a[apos];
apos --;
} else {
a[i] = b[bpos];
bpos --;
}
}
試過幾個edge case都沒問題,這應該就是O(n)的一個解答了
--
Tags:
海外工作
All Comments
Related Posts
H4疑問及帶貓過去的問題
By Brianna
at 2007-12-15T06:09
at 2007-12-15T06:09
PTO的算法
By Leila
at 2007-12-13T11:43
at 2007-12-13T11:43
PTO的算法
By Vanessa
at 2007-12-13T10:27
at 2007-12-13T10:27
談談醫療保險
By Rachel
at 2007-12-12T12:28
at 2007-12-12T12:28
轉換工作注意事項
By Joseph
at 2007-12-12T08:53
at 2007-12-12T08:53