腾讯//合并两个有序数组

 

给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 使得 num1 成为一个有序数组。

说明:

  • 初始化 nums1 和 nums2 的元素数量分别为 m 和 n
  • 你可以假设 nums1 有足够的空间(空间大小大于或等于 m + n)来保存 nums2 中的元素。

示例:

输入:
nums1 = [1,2,3,0,0,0], m = 3
nums2 = [2,5,6],       n = 3

输出: [1,2,2,3,5,6]
class Solution {
public:
    void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
        int num[m+n];
        int i=0, j=0, k=0;
        while(i<m&&j<n){
            if(nums1[i]<=nums2[j])
                num[k++] = nums1[i++];
            else
                num[k++] = nums2[j++];
        }
        while(i<m) num[k++] = nums1[i++];
        while(j<n) num[k++] = nums2[j++];
        copy(num,num+m+n,nums1.begin());
    }
};
class Solution {
public:
    void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
        int num[m+n];//新建一个数组,对nums1和nums2排序,排完序赋值给nums1
        int i = 0,j = 0,k = 0;
        while(i<m && j<n){
            if(nums1[i] <= nums2[j])
                num[k++] = nums1[i++];
            else
                num[k++] = nums2[j++];
        }
        while(i < m) num[k++] = nums1[i++];
        while(j < n) num[k++] = nums2[j++];
        copy(num,num+m+n,nums1.begin());
    }
};
class Solution {
public:
    void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
        int k=m+n;
        while(m>0 && n>0){
            if(nums1[m-1] >= nums2[n-1]){
                nums1[k-1] = nums1[m-1];
                --k; 
                --m;
            }
            else{
                nums1[k-1] = nums2[n-1];
                --k;
                --n;
            }
        }
        while(n > 0){
            nums1[k-1] = nums2[n-1];
            --k;
            --n;
        }
    }
};

 

posted @ 2018-10-23 16:49  strawqqhat  阅读(92)  评论(0编辑  收藏  举报
#home h1{ font-size:45px; } body{ background-image: url("放你的背景图链接"); background-position: initial; background-size: cover; background-repeat: no-repeat; background-attachment: fixed; background-origin: initial; background-clip: initial; height:100%; width:100%; } #home{ opacity:0.7; } .wall{ position: fixed; top: 0; left: 0; bottom: 0; right: 0; } div#midground{ background: url("https://i.postimg.cc/PP5GtGtM/midground.png"); z-index: -1; -webkit-animation: cc 200s linear infinite; -moz-animation: cc 200s linear infinite; -o-animation: cc 200s linear infinite; animation: cc 200s linear infinite; } div#foreground{ background: url("https://i.postimg.cc/z3jZZD1B/foreground.png"); z-index: -2; -webkit-animation: cc 253s linear infinite; -o-animation: cc 253s linear infinite; -moz-animation: cc 253s linear infinite; animation: cc 253s linear infinite; } div#top{ background: url("https://i.postimg.cc/PP5GtGtM/midground.png"); z-index: -4; -webkit-animation: da 200s linear infinite; -o-animation: da 200s linear infinite; animation: da 200s linear infinite; } @-webkit-keyframes cc { from{ background-position: 0 0; transform: translateY(10px); } to{ background-position: 600% 0; } } @-o-keyframes cc { from{ background-position: 0 0; transform: translateY(10px); } to{ background-position: 600% 0; } } @-moz-keyframes cc { from{ background-position: 0 0; transform: translateY(10px); } to{ background-position: 600% 0; } } @keyframes cc { 0%{ background-position: 0 0; } 100%{ background-position: 600% 0; } } @keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } } @-webkit-keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } } @-moz-keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } } @-ms-keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } }