Merge Sorted Array
2015-06-09 09:26
429 查看
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.
思路:从后向前合并,这样就不需要额外的存储空间,时间复杂度为O(m+n)也非常好
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.
思路:从后向前合并,这样就不需要额外的存储空间,时间复杂度为O(m+n)也非常好
public class Solution { public void merge(int[] nums1, int m, int[] nums2, int n) { int count1 = m-1; int count2 = n-1; int count3 = m+n-1; while(count1>=0 && count2>=0) { if(nums1[count1]>=nums2[count2]) { nums1[count3] = nums1[count1]; count3--; count1--; } else { nums1[count3] = nums2[count2]; count3--; count2--; } } if(count2<0) { return; } else{ for(int i=0;i<=count2;i++) { nums1[i] = nums2[i]; } return; } } }
相关文章推荐
- Excel Sheet Column Number
- 解决Cannot change version of project facet Dynamic web module to 2.5
- 2012 Microsoft Intern Hiring Written Test
- android按back键退出程序时,实现“再按一次退出”的功能
- 股票评级 买入 增持 强烈推荐 推荐 的含义和关系
- 非常简单的js双向绑定框架(一)
- 为什么要学习C语言
- 前端优化:图片预加载
- 管线相关命令(cut grep sort wc uniq)
- 成为java高手的八大条件
- PostgreSQL安装详细步骤(linux)
- LVS内核中的连接调度算法
- 深入理解Java:注解(Annotation)自定义注解入门
- checkbox属性checked="checked"通过js已设置,但是不勾选
- Android 圆角矩形ImageView
- 认识ExtJs的开发包(基础必备)
- Eclipse启动的时候提示:Failed to load JavaHL Library.
- 推荐几个非常有用的工具
- Cocos2d-x利用jni调用java层代码
- 国内PaaS概述和EEPlat定位