lintcode:两数组的交二
来源:互联网 发布:mac查找jdk安装路径 编辑:程序博客网 时间:2024/05/01 22:14
刚开始无法理解这种写法,后来越看越觉得写得好,这种思维方式让人佩服
public class Solution { /** * @param nums1 an integer array * @param nums2 an integer array * @return an integer array */ public int[] intersection(int[] nums1, int[] nums2) { // Write your code here if (nums1.length == 0 || nums2.length == 0) { return new int[]{}; } Arrays.sort(nums1); Arrays.sort(nums2); List<Integer> list = new ArrayList<Integer>(); int index1 = 0; int index2 = 0; while (index1 < nums1.length && index2 < nums2.length) { if (nums1[index1] == nums2[index2]) { list.add(nums1[index1]); index1++; index2++; } else { if (nums1[index1] < nums2[index2]) { index1++; } else { index2++; } } } int[] nums = new int[list.size()]; for (int j = 0; j < list.size(); j++) { nums[j] = list.get(j); } return nums; }}
来源:http://blog.csdn.net/qq_26971803/article/details/52135673
0 0
- lintcode:两数组的交二
- LintCode:两数组的交
- LintCode:两数组的交
- 两数组的交-LintCode
- lintcode 两数组的交
- LintCode-两数组的交
- Lintcode 两数组的交
- LintCode 两数组的交
- LintCode(两数组的交)
- 两数组的交-LintCode
- LintCode(easy)两数组的交
- LintCode:两数组的交 II
- LintCode 547 两数组的交
- LintCode 548 两数组的交 II
- LintCode【两数组的交II】
- 【lintcode笔记】两数组的交
- 【lintcode笔记】两数组的交 II
- LintCode-两数组的交 II
- 总是找不到工作?因为你老犯这些错!
- 初步探讨Zigbee之外设红外检测
- 网上找的jqurey中ajax5中用法
- css3中的@font-face的用法(定义多个规则)
- JAVA多线程实现的三种方式
- lintcode:两数组的交二
- Django指令总结
- 第十五周OJ——字符串的修改
- [CSS]CSS浮动float详解(三):清除浮动方案
- Input 介绍
- 链栈的基本操作
- Android Settings和SettingsProvider源码分析与修改
- 正常权限
- 大型网站架构:消息队列(二)