两个有序数组合并,并去重复
2011-10-20 20:48
295 查看
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
class ArraySort {
// 两个有序数组的合并函数
public static int[] MergeList(int a[], int b[]) {
int result[];
if (checkSort(a) && checkSort(b)) // 检查传入的数组是否是有序的
{
result = new int[a.length + b.length];
int i = 0, j = 0, k = 0; // i:用于标示a数组 j:用来标示b数组 k:用来标示传入的数组
while (i < a.length && j < b.length)
if (a[i] <= b[j]) {
if (k == 0 || result[k - 1] != a[i]) //去重复
result[k++] = a[i];
i++;
} else {
if (k == 0 || result[k - 1] != b[j]) //去重复
result[k++] = b[j];
j++;
}
/* 后面连个while循环是用来保证两个数组比较完之后剩下的一个数组里的元素能顺利传入 */
while (i < a.length) {
if (k == 0 || result[k - 1] != a[i]) //去重复
result[k++] = a[i];
i++;
}
while (j < b.length) {
if (k == 0 || result[k - 1] != b[j]) //去重复
result[k++] = b[j];
j++;
}
return result;
} else {
System.out.print("非有序数组,不可排序!");
return null;
}
}
// 检查数组是否是顺序存储的
public static boolean checkSort(int a[]) {
boolean change = true; // 这个标志位是一种优化程序的方法,可以看看我写的冒泡排序优化就会明白了
for (int i = 0; i < a.length - 1 && change; i++) {
for (int j = i + 1; j < a.length; j++)
if (a[j - 1] > a[j])
return false;
else
change = false;
}
return true;
}
// 打印函数
public static void print(int b[]) {
for (int i = 0; i < b.length; i++) {
System.out.print(b[i] + (i % 10 == 9 ? "\n" : "\t"));
System.out.println("\n");
}
}
public static void main(String args[]) {
int a[] = { 1, 2, 2, 3, 5, 6, 7, 7 };
int b[] = { 1, 2, 4, 5, 8, 8, 9, 10, 11, 12, 12, 13, 14 };
int c[] = MergeList(a, b);
if (c != null)
print(c);
else
System.out.println("");
/*
* int t[]={1,4,5,6,7,8}; System.out.println(checkSort(t));
*/
}
}
import java.util.Set;
import java.util.TreeSet;
class ArraySort {
// 两个有序数组的合并函数
public static int[] MergeList(int a[], int b[]) {
int result[];
if (checkSort(a) && checkSort(b)) // 检查传入的数组是否是有序的
{
result = new int[a.length + b.length];
int i = 0, j = 0, k = 0; // i:用于标示a数组 j:用来标示b数组 k:用来标示传入的数组
while (i < a.length && j < b.length)
if (a[i] <= b[j]) {
if (k == 0 || result[k - 1] != a[i]) //去重复
result[k++] = a[i];
i++;
} else {
if (k == 0 || result[k - 1] != b[j]) //去重复
result[k++] = b[j];
j++;
}
/* 后面连个while循环是用来保证两个数组比较完之后剩下的一个数组里的元素能顺利传入 */
while (i < a.length) {
if (k == 0 || result[k - 1] != a[i]) //去重复
result[k++] = a[i];
i++;
}
while (j < b.length) {
if (k == 0 || result[k - 1] != b[j]) //去重复
result[k++] = b[j];
j++;
}
return result;
} else {
System.out.print("非有序数组,不可排序!");
return null;
}
}
// 检查数组是否是顺序存储的
public static boolean checkSort(int a[]) {
boolean change = true; // 这个标志位是一种优化程序的方法,可以看看我写的冒泡排序优化就会明白了
for (int i = 0; i < a.length - 1 && change; i++) {
for (int j = i + 1; j < a.length; j++)
if (a[j - 1] > a[j])
return false;
else
change = false;
}
return true;
}
// 打印函数
public static void print(int b[]) {
for (int i = 0; i < b.length; i++) {
System.out.print(b[i] + (i % 10 == 9 ? "\n" : "\t"));
System.out.println("\n");
}
}
public static void main(String args[]) {
int a[] = { 1, 2, 2, 3, 5, 6, 7, 7 };
int b[] = { 1, 2, 4, 5, 8, 8, 9, 10, 11, 12, 12, 13, 14 };
int c[] = MergeList(a, b);
if (c != null)
print(c);
else
System.out.println("");
/*
* int t[]={1,4,5,6,7,8}; System.out.println(checkSort(t));
*/
}
}
相关文章推荐
- 两个有序数组的合并排序,Java代码实现,并去重复,考虑空间利用率问题
- 合并有重复元素的两个有序数组,输出无重复元素
- 两个有序数组的合并排序,Java代码实现,并去重复,考虑空间利用率问题
- JAVA合并两个有序数组并去除重复元素。
- 程序员面试题目总结--数组(二)【二分查找、找出给定数字出现次数、两个有序整型数组交集、找出数组中唯一的重复元素、判断数组中的数值是否连续相邻】
- Merge Sorted Array 合并两个有序数组 @LeetCode
- C++两个有序数组合并
- iOS 笔试题~两个有序数组合并成一个有序数组
- LeetCode - 88. 合并两个有序数组
- 两个有序数组的合并,python版 推荐
- 两个有序数组合并成一个有序数组
- 合并两个有序的数列并删除重复数据
- 算法:两个有序数组的合并
- leetcode之数组类之数组的旋转与分治类-----OJ 189/33/81/153/154 数组旋转 旋转数组搜索 88 有序数组合并 4 两个有序数组寻找第K个元素/中位数 35 寻找插入位置
- 1124: 两个有序数组合并
- c# 合并两个有序数组
- 算法 - 合并两个有序数组为一个有序数组
- 替换空格及合并两个有序的数组
- Interview Q&A - 合并两个有序数组为一个有序数组(C++)
- PHP实现合并两个有序数组的方法分析