重慶分公司,新征程啟航
為企業提供網站建設、域名注冊、服務器等服務
為企業提供網站建設、域名注冊、服務器等服務
88. 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.
問題描述:將兩個有序數組合并成一個有序數組。
思路:創建第三個數組,將其它兩個數組有序的插入第三個數組中。然后根據需求變化。
代碼如下:
class Solution { public: void merge(vector& nums1, int m, vector & nums2, int n) { vector result; int i= 0; int j=0; while( (i < m) && (j < n)) { if(nums1[i] <= nums2[j]) { result.push_back(nums1[i]); i++; } else { result.push_back(nums2[j]); j++; } } if(i < m) { for(;i < m; i++ ) { result.push_back(nums1[i]); } } if(j < n) { for(;j < n; j++) { result.push_back(nums2[j]); } } swap(result,nums1); } };
2016-08-05 23:36:14
另外有需要云服務器可以了解下創新互聯scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業上云的綜合解決方案,具有“安全穩定、簡單易用、服務可用性高、性價比高”等特點與優勢,專為企業上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。