Leetcode Problem.88 —Merge Sorted Array
2015-06-03 15:24
393 查看
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 nums1and nums2 are m and n respectively.
My C++ solution!
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 nums1and nums2 are m and n respectively.
My C++ solution!
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) { if(m==0&&n==0) return; else if(m==0) nums1=nums2; else { int i=0; int j=0; while(j<n) { if(nums2[j]<=nums1[i]) { nums1.insert(nums1.begin()+i,nums2[j]); i++; j++; } else if(i==m+j-1) { nums1.insert(nums1.begin()+m+j,nums2.begin()+j,nums2.end()); j=n; } else i++; } } nums1.resize(m+n); }
相关文章推荐
- Oracle 11gR2 使用RMAN Duplicate复制数据库
- Windows GetAsyncKeyState(vk_code)
- NETBIOS协议
- DRUID连接池的使用
- springmvc下的基于token的防重复提交
- Java软件开发中的几种认识误区
- Leetcode Problem.83—Remove Duplicates from Sorted List
- ueditor富文本
- C++中const、volatile、mutable的用法
- 基于DRBD实现高可用MariaDB
- Centos6.6上rsyslog一些用法和扩展
- Some paper related to Image Deblurring
- switch语句中得case
- linux centos6.5安装KVM
- RDIFramework.NET ━ .NET快速信息化系统开发框架钜献 V2.9 版本震撼发布
- 异常日志以及非异常日志记录方法
- bzoj 1336 最小圆覆盖
- 使用pt-online-schema-change 修复主从数据表数据不一致
- C语言字节对齐
- Leetcode Problem.67—Add Binary