zoukankan      html  css  js  c++  java
  • Merge Sorted Array

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.

    Note:
    You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The number of elements initialized in nums1 and nums2 are m and n respectively.

    分析: 注意理解题目 从num1中取前m个 从num2中取前n个 然后放入num1中 O(N)的复杂度即可

    class Solution {
    public:
        void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
            int i=0;
            int j=0;
            for(; j<n;j++){
                for(; i<m+j;i++){
                    if(nums2[j]<nums1[i])
                        break;
                }
                if(i<m+j){
                    nums1.insert(nums1.begin()+i,nums2[j]);
                    nums1.pop_back();
                }
                else
                    break;
            }
           
            while(j<n){
                nums1.insert(nums1.begin()+m+j-1,nums2[j++]);
                nums1.pop_back();
            }
            
            
        }
    };
  • 相关阅读:
    ajax小白理解
    Once more
    win滚动条样式修改
    NOIP2018游记
    Stirling数笔记
    【Start From Here】HNOI2018 滚粗记
    6面相对象
    5方法定义及调用
    Java4数组
    Java3流程控制语句
  • 原文地址:https://www.cnblogs.com/willwu/p/6254288.html
Copyright © 2011-2022 走看看