• 合并两个有序数组为一个新的有序数组


    1. public class MergeTwoSortedArrays {  
    2.     public static int[] merge(int[] a, int[] b) {  
    3.         int lena = a.length;  
    4.         int lenb = b.length;  
    5.         int[] c = new int[lena + lenb];  
    6.         int i = 0, j = 0, k = 0;//分别代表数组a ,b , c 的索引  
    7.         while (i < lena && j < lenb) {  
    8.             if (a[i] < b[j])  
    9.                 c[k++] = a[i++];  
    10.             else  
    11.                 c[k++] = b[j++];  
    12.         }  
    13.         while (i < lena)  
    14.             c[k++] = a[i++];  
    15.         while (j < lenb)  
    16.             c[k++] = b[j++];  
    17.         return c;  
    18.     }  
    19.   
    20.     public static void main(String[] args) {  
    21.         int[] c = merge(new int[] { 1, 2, 3, 4 }, new int[] { 0, 2, 4, 5,  
    22.                 6, 7, 8 });  
    23.         for (int i = 0; i < c.length; i++)  
    24.             System.out.println(c[i]);  
    25.     }  
    26.   
    27. }  合并两个有序数组为一个新的有序数组
  • 相关阅读:
    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  润新知