-
合并两个有序数组为一个新的有序数组
- public class MergeTwoSortedArrays {
- public static int[] merge(int[] a, int[] b) {
- int lena = a.length;
- int lenb = b.length;
- int[] c = new int[lena + lenb];
- int i = 0, j = 0, k = 0;
- while (i < lena && j < lenb) {
- if (a[i] < b[j])
- c[k++] = a[i++];
- else
- c[k++] = b[j++];
- }
- while (i < lena)
- c[k++] = a[i++];
- while (j < lenb)
- c[k++] = b[j++];
- return c;
- }
-
- public static void main(String[] args) {
- int[] c = merge(new int[] { 1, 2, 3, 4 }, new int[] { 0, 2, 4, 5,
- 6, 7, 8 });
- for (int i = 0; i < c.length; i++)
- System.out.println(c[i]);
- }
-
- } 合并两个有序数组为一个新的有序数组
-
相关阅读:
Date
Math
封装实参的对象 arguments
函数方法call()和apply()
执行上下文栈
原型(prototype属性)和原型链。 重要!!!
android studio for Mac快捷键大全
软件的横竖屏切换
Android XML文件中@id和@+id的区别
java中的Iterator<E>
-
原文地址:https://www.cnblogs.com/yangchunchun/p/7458570.html
Copyright © 2020-2023
润新知