由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 有好的merge有序数组算法么
相关主题
merge两个有序数组求两个等长有序数组的median的细节
max sub vector sum 问题讨论一题,去除有序数组的重复元素
问一题:merge两个有序数组请教一题,求两个不等长的有序数组的median
请教一道Leetcode 题, 多谢问一道题
请教leetcode一道题问道小学题:两等长有序数组,求第k个数
一个小公司面经面试题:两个有序数组中的最小差值
求一下这题解法。请教大家一个算法的面试题目
LinkIn面经LC有序数组删重复元素的题怎么最快?
相关话题的讨论汇总
话题: integer话题: new话题: int话题: merge话题: static
进入JobHunting版参与讨论
1 (共1页)
y***m
发帖数: 7027
1
简单java的,有更优化的么,thx!
private static void merge() {
Integer A[] = new Integer[] { 1, 5, 8, 14, 25 };
Integer B[] = new Integer[] { 3, 4, 7, 17, 27 };
int k = A.length + B.length;
Integer C[] = new Integer[k];
int i = 0, j = 0, m = 0;
while (m < k-1) {
if (A[i] > B[j]) {
C[m] = B[j];
j++;
} else {
C[m] = A[i];
i++;
}
log.info( " " + C[m] );
m++;
}
}
g***s
发帖数: 3811
2
your codes will overflow【 在 yesim (yesim) 的大作中提到: 】
p****y
发帖数: 405
3
为什么?看着对。

【在 g***s 的大作中提到】
: your codes will overflow【 在 yesim (yesim) 的大作中提到: 】
y***m
发帖数: 7027
4
跑通的,有测试数据?

your codes will overflow【 在 yesim (yesim) 的大作中提到: 】

【在 g***s 的大作中提到】
: your codes will overflow【 在 yesim (yesim) 的大作中提到: 】
q*****9
发帖数: 85
5
check if either one already empty
g***s
发帖数: 3811
6
please change one line to test:
Integer B[] = new Integer[] { 3, 4, 7, 17, 27,30 };

【在 y***m 的大作中提到】
: 跑通的,有测试数据?
:
: your codes will overflow【 在 yesim (yesim) 的大作中提到: 】

p****y
发帖数: 405
7
同意,呵呵,2个都要check是否empty。
empty之后就是copy另外一个剩下的。

【在 q*****9 的大作中提到】
: check if either one already empty
n**z
发帖数: 155
8
如果第一个数组跑晚了。你的循环还会去比较下一组。
你必须检查index或者在数组的最后设置无限大的最后一个元素

【在 p****y 的大作中提到】
: 为什么?看着对。
y***m
发帖数: 7027
9
哦... 这样可以?
thx!
private static void merge2() {
Integer B[] = new Integer[] { 1, 3, 8, 14, 25 };
Integer A[] = new Integer[] { 3, 4, 7, 17, 27, 30 };
int k = A.length + B.length;
Integer C[] = new Integer[k];
int i = 0, j = 0, m = 0;
while (m < k - 1) {
if (j < B.length - 1 && (A[i] > B[j] || i > A.length - 2)) {
C[m] = B[j];
j++;
} else {
C[m] = A[i];
i++;
}
log.info(" " + C[m]);
m++;
}
}

【在 g***s 的大作中提到】
: please change one line to test:
: Integer B[] = new Integer[] { 3, 4, 7, 17, 27,30 };

g***s
发帖数: 3811
10
try
Integer A[] = new Integer[]{1};

【在 y***m 的大作中提到】
: 哦... 这样可以?
: thx!
: private static void merge2() {
: Integer B[] = new Integer[] { 1, 3, 8, 14, 25 };
: Integer A[] = new Integer[] { 3, 4, 7, 17, 27, 30 };
: int k = A.length + B.length;
: Integer C[] = new Integer[k];
: int i = 0, j = 0, m = 0;
: while (m < k - 1) {
: if (j < B.length - 1 && (A[i] > B[j] || i > A.length - 2)) {

p****y
发帖数: 405
11
呵呵。

【在 g***s 的大作中提到】
: try
: Integer A[] = new Integer[]{1};

r***y
发帖数: 4379
12
do not use Integer
use int instead
1 (共1页)
进入JobHunting版参与讨论
相关主题
LC有序数组删重复元素的题怎么最快?请教leetcode一道题
问道题一个小公司面经
问个问题 求missing number求一下这题解法。
问一个static variable上锁的问题LinkIn面经
merge两个有序数组求两个等长有序数组的median的细节
max sub vector sum 问题讨论一题,去除有序数组的重复元素
问一题:merge两个有序数组请教一题,求两个不等长的有序数组的median
请教一道Leetcode 题, 多谢问一道题
相关话题的讨论汇总
话题: integer话题: new话题: int话题: merge话题: static