读书人

lt;找工作 6gt;leetcode Median of Two So

发布时间: 2013-01-01 14:04:19 作者: rapoo

<找工作 六>leetcode Median of Two Sorted Arrays

http://www.leetcode.com/onlinejudge

Median of Two Sorted Arrays

代码风格很乱,将就看吧,

写完才发现,写的有问题,没必要对end做操作,只操作start就可以了,类似归并

?

/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubint A[] = {};int B[] = { 1, 2, 3, 4, 5 };double c = new testMedianofTwoSortedArrays().findMedianSortedArrays(A,B);System.out.println(c);}public double findMedianSortedArrays(int A[], int B[]) {// Start typing your Java solution below// DO NOT write main() functionint startA = 0;int startB = 0;int tempS = 0, tempE = 0;double end = ((double) A.length + B.length) / 2;double result = 0;while (startA + startB < end) {if (startA >= A.length) {tempS = B[startB];startB++;} else if (startB >= B.length) {tempS = A[startA];startA++;} else if (A[startA] > B[startB]) {tempS = B[startB];startB++;} else {tempS = A[startA];startA++;}}if ((A.length + B.length) % 2 == 1) {tempE = tempS;} else if (startA >= A.length) {tempE = B[startB];} else if (startB >= B.length) {tempE = A[startA];} else if (A[startA] > B[startB]) {tempE = B[startB];} else {tempE = A[startA];}return ((double) tempS + tempE) / 2;}}
?

读书人网 >编程

热点推荐