[编程题]2
  • 热度指数:413 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
有两个数组ab,数组a的元素是升序排列(从小到大),数组b的元素是降序排列(从大到小),请写出算法,将这两个数组合并成升序排列的数组。
示例1

输入

[1,2,3],[6,5,4]

输出

[1,2,3,4,5,6]
[Python3] 用sort()不怕把面试官气岔气吗?🤣🤣🤣
class Solution:
    def sort(self , a , b ):
        a, c = a[::-1], []
        while a and b:
            if a[-1] < b[-1]:
                c.append(a.pop())
            else:
                c.append(b.pop())
        if not a:
            c.extend(b)
        else:
            c.extend(a)
        return c


发表于 2021-07-26 17:30:47 回复(0)
投机取巧
#
# 数组排序
# @param a int整型一维数组 数组a升序
# @param b int整型一维数组 数组b降序
# @return int整型一维数组
#
class Solution:
    def sort(self , a , b ):
        # write code here
        a.extend(b) 
        a.sort()
        return a

发表于 2021-06-02 19:26:29 回复(0)
vector<int> sort(int* a, int aLen, int* b, int bLen) {
    vector<int>r(aLen + bLen);//开辟一个数组
    int index1 = 0;
    int index2 = bLen - 1;
    int cur;
    int mm = 0;
    while (index1 <= aLen - 1 || index2 >= 0)
    {
        if (index1 == aLen)
        {
            cur = b[index2--];
        }
        else if (index2 == -1)
        {
            cur = a[index1++];
        }
        else if (a[index1] < b[index2])
        {
            cur = a[index1++];
        }
        else
        {
            cur = b[index2--];
        }
        r[mm++] = cur;
        cout << cur << " ";
        cout << endl;
    }
    return r;
}
发表于 2022-07-07 11:44:44 回复(0)
import java.util.*;
 
 
public class Solution {
    /**
     * 数组排序
     * @param a int整型一维数组 数组a升序
     * @param b int整型一维数组 数组b降序
     * @return int整型一维数组
     */
    public int[] sort (int[] a, int[] b) {
        // write code here
        String str="";
        for(int i = 0;i< a.length;i++){
            str+=String.valueOf(a[i])+",";
        }
        for(int j =0;j<b.length;j++){
            str+=String.valueOf(b[j])+",";
        }
        String [] c = str.split(",");
        int [] d = new int [c.length];
        for(int k =0;k<c.length;k++){
            d[k]= Integer.parseInt(c[k]);
        }
         
        Arrays.sort(d);
        return d;
         
         
    }
}

发表于 2021-11-13 00:23:31 回复(0)