1. 程式人生 > >兩個有序數組合併成一個大的有序陣列

兩個有序數組合併成一個大的有序陣列

package com.test;

public class use7 {
    public static void main(String[] args) {
        int[] a = { 3, 4, 7, 9 };
        int[] b = { 1, 5, 6, 77 };
        int[] result = MergeList(a, b);
        for (int i : result) {
            System.out.println(i);
        }
    }
    public static int[] MergeList(int a[], int b[]) {
        int result[];
        // 定義一個新陣列,長度為兩個陣列長度之和
        result = new int[a.length + b.length];
        // i:a陣列下標 j:b陣列下標 k:新陣列下標
        int i = 0, j = 0, k = 0;
        // 按位迴圈比較兩個陣列,較小元素的放入新陣列,下標加一(注意,較大元素對應的下標不加一),直到某一個下標等於陣列長度時退出迴圈
        while (i < a.length && j < b.length)
            if (a[i] <= b[j]) {
                result[k++] = a[i++];
                System.out.print(result);
                System.out.println();
            } else {
                result[k++] = b[j++];
            }
        /*
         * 後面連個while迴圈是用來保證兩個陣列比較完之後剩下的一個數組裡的元素能順利傳入 *
         * 此時較短陣列已經全部放入新陣列,較長陣列還有部分剩餘,最後將剩下的部分元素放入新陣列,大功告成
         */
        while (i < a.length)
            result[k++] = a[i++];
        while (j < b.length)
            result[k++] = b[j++];
        return result;
    }
}

相關推薦

序數合併一個有序陣列

package com.test;public class use7 {    public static void main(String[] args) {        int[] a = { 3, 4, 7, 9 };        int[] b = { 1, 5,

序數合併一個有序陣列,演算法複雜度O(N)

/** * */ /** * @author jueying: * @version 建立時間:2018-10-22 下午01:32:44 * 類說明 */ /** * @author jueying * */ public class Test4 {

Java將有序連結串列合併一個有序連結串列、將序數合併一個有序陣列

有序連結串列合併 題目:已知兩個連結串列head1和head2各自有序,請把它們合併成一個連結串列依然有序。結果連結串列要包含head1和head2的所有節點,即使節點值相同。 分析:此題目使

java 序數合併一個有序陣列(時間複雜度低)

預設一般會採用陣列先合併,在排序 時間複雜度會在o(n) -o(n*n) 之間 我想了個其他的思路 對陣列1 和陣列2 元素從頭開始進行一次對比,小的放入結果集合, 直到兩個陣列的元素都加入結果集合 這樣的時間複雜度 在o(n) 只要比較兩個陣列較短的長度的次數 程式碼

遞增序數合併一個遞增陣列

public static int[] MergeList(int a[],int b[]) { int result[]; // 定義一個新陣列,長度為兩個陣列長度之和

算法 - 序數組合並一個序數

== out while循環 有序數組 oid 是否 打印 sort nbsp //兩個有序數組的合並函數 public static int[] MergeList(int a[],int b[]) { int result[];

88. Merge Sorted Array【leetcode】演算法,java將序數合併到一個數

88. Merge Sorted Array Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: You may as

合並序數一個新的序數

int tro pre pri ack ati 數組 data- string 題目:有兩個有序數組a,b,現須要將其合並成一個新的有序數組。 簡單的思路就是先放到一個新的數組中,再排序。可是這種沒體現不論什麽算法,這裏考的不是高速排序等排序算法。關鍵應該是怎樣利

[LeetCode]Median of Two Sorted Arrays 二分查找序數的第k數(中位數)

大於 data div ble 關系 操作 spa 兩個 -1 二分。情況討論 因為數組有序,所以能夠考慮用二分。通過二分剔除掉肯定不是第k位數的區間。如果數組A和B當前處理的下標各自是mid1和mid2。則 1、假設A[mid1]<B[mid2], ①

merge-sorted-array——合並序數

nts from -a else array integer ber sum initial Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may

序數的中位數或者第k小元素(轉載)

href 數組 lan get .cn sdoi com 第k小元素 .html http://www.cnblogs.com/TenosDoIt/p/3554479.html http://www.cnblogs.com/TenosDoIt/p/3675220.htm

序數的中位數(4. Median of Two Sorted Arrays)

排序 font float 序列 大小 width 技術 display 個數 先吐槽一下,我好氣啊,想了很久硬是沒有做出來,題目要求的時間復雜度為O(log(m+n)),我猜到了要用二分法,但是沒有想到點子上去。然後上網搜了一下答案,感覺好有罪惡感。 題目原型 正確的

面試題----合並序數

合並 printf 面試 有序數組 color merge set ++ style #include<stdio.h> #include<string.h> #include<stdlib.h> void merge(int a[]

[LeetCode] 4. Median of Two Sorted Arrays 序數的中位數

數據 pub art cti AI nts highlight sta binary There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of t

合並序數

i++ lis task merge args () pro bool 兩個 using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syste

【LeetCode】88.合並序數

for etc leet col lse else while num oid class Solution { public: void merge(vector<int>& nums1, int m, vector<int>&a

LeetCode--088--合並序數

void self int pre 有序 not col leetcode -- 方法1: 1 class Solution(object): 2 def merge(self, nums1, m, nums2, n): 3 """ 4

LeetCode88合並序數

ble strong col break bre return 細節 -a problem 未經博主同意,禁止瞎JB轉載。 LeetCode88合並兩個有序數組 https://leetcode-cn.com/problems/merge-sorted-array/desc

python一維列表合併一個二維列表

>>> list1 = [1,2,3,4,4] >>> list2 = [2,3,4,5,2] >>> z = list(zip(list1,list2)) >>> z [(1, 2), (2, 3), (3, 4)

C++將遞增連結串列 合併 一個遞增連結串列 不佔用額外的空間

/*create by tyy*/ #include <iostream> #include <string> using namespace std; #define ok 1 #define error -1 #define overflow -