有序数组合并
2015-07-14 12:27
267 查看
package com.hhx; /** * Created by hyson on 15/7/14. */ public class CombinArray { public static void main(String[] args) { int[] a = {-1, -3, 2, 4}; int[] b = {3, 5, 6}; int[] result = combineArray(a, b); for (int i = 0; i < result.length; i++) { System.out.print(result[i] + " "); } } public static int[] combineArray(int[] a, int[] b) { int[] result = new int[a.length + b.length]; int i = 0, j = 0, k = 0; while (i < a.length && j < b.length) { if (a[i] < b[j]) { result[k++] = a[i++]; } else { result[k++] = b[j++]; } } while (i < a.length) { result[k++] = a[i++]; } while (j < b.length) { result[k++] = b[j++]; } return result; } }
相关文章推荐
- Java面试题全集(上)
- java实现日期拆分的方法
- 中文 iOS/Mac 开发博客列表
- HDU-1147-Pick-up sticks
- 虚幻4创建自己的UMG类别
- object-c中对文件操作
- 根据一段代码理清一些变量,常量,数组,指针的关系及理解
- 解读AFNetworking中Demo的MVC
- C 判断路径存在
- 七天学会ASP.NET MVC (六)――线程问题、异常处理、自定义URL
- 浅析MVC和说媒的过程
- hdu 1284 钱币兑换问题
- Eclipse 如何同时打开查看多个文件
- 一个点的经度和纬度,以这个点为圆心,1000米为半径,最大的经度和纬度,最小的经度和纬度
- xcode项目编译C/C++文件could not build module foundation
- hdu 1284 钱币兑换问题
- mongodb数据库的启动和停止
- 分享个刚写好的 android 的 ListView 动态加载类,功能全而代码少。
- ubuntu14.04 64bit安装skype4.3
- 公钥、私钥、SSL趣解