leetcode88:Merge Sorted Array
2016-05-06 23:02
543 查看
package com.leetcode;
import java.util.Arrays;
public class Solution88 {
//a=[1,2,6] b=[2,4,8] 合并后c=[1,2,2,4,6,8],由于题目假设a的数组大小是大于或等于a+b,所以a最终是输出结果
public static void merge(int [ ] nums1, int m, int [ ] nums2, int n){
int index = m+n-1;
int i = m-1;
int j = n-1;
while( i >= 0 && j >= 0 ){
if( nums1[ i ] < nums2[ j ] )
nums1[ index-- ] = nums2[ j-- ];
else
nums1[ index-- ] = nums1[ i-- ];
}
// 如果此时nums2还没遍历完
while( j >= 0){
nums1[ index-- ] = nums2[ j-- ];
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int [ ] nums1 =new int[20];
for(int i = 0; i< 6; i++)
nums1[ i ] = i;
int [ ] nums2 = {2,4,5,6,8,9};
Solution88.merge(nums1, 6, nums2, 6);
System.out.println(Arrays.toString(nums1));
}
}
相关文章推荐
- 柔性数组
- Android-文件存储
- 快速找出一个数组中的两个数字,让这两个数字之和等于一个给定的值
- Ubuntu Kylin 16.04 LTS 安装TensorFlow 初体验
- 侦探游戏:不服来战
- Android05-UI02布局,自定义控件,ListView
- 剑指Offer--038-数字在排序数组中出现的次数
- Makefile中用宏定义进行条件编译
- [最短路] 恶魔城 (satanic)
- linux命令(45):diff命令
- Scrum项目1.0
- Oracle学习系列5
- jdk源码分析之AbstractMap
- Laravel错误解决方案
- 部分 CSS 的浏览器兼容处理
- linux与Windows的不同
- 架构中使用反射机制
- Hash算法专题
- Oracle学习系列4
- [置顶] jQuery判断checkbox,radio是否选中