Merge Sorted Array
2015-05-28 22:00
239 查看
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.
思路:类似于插入排序
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.
思路:类似于插入排序
public class Solution { public void merge(int[] nums1, int m, int[] nums2, int n) { if(m==0) { for(int k=0;k<n;k++) nums1[k]=nums2[k]; } for(int i=0;i<n;i++) { int j=m-1+i; for(;j>=0;j--) { if(nums1[j]>nums2[i]) nums1[j+1]=nums1[j]; else break; } nums1[j+1]=nums2[i]; } } }
相关文章推荐
- Java Servlet学习笔记(五)Servlet服务器Http响应
- 在Visual Studio 的 “一般处理程序 ” .ashx 文件中如何创建Session 对象
- App项目开发流程图解
- 黑马程序员----反射
- iPhone屏幕知识点解析
- A*寻路算法浅析
- [翻译] JHChainableAnimations
- 团队项目 SRS文档
- 一个线程池解决epoll socket队列的方法
- Cpp Primer<<学习函数-局部对象、内联函数、类的成员函数_1
- 一些项目——FatMouse' Trade
- Hadoop的调度器总结
- 第二次冲刺(3)
- 个人日常linux相关工作问题汇总
- data mining - 实用机器学习与技术 读书笔记(七)
- 丑数
- QQ为什么是棵常青树:极客精神+匠人品质
- 轻量级javaEE SSH 01: 概述 环境与工具篇
- UVA125 - Numbering Paths(floyd)
- 模型类中 Parcelable 接口使用