Comments (3)
解题思路
- 在假定数组已有序的情况下, 可以直接使用双指针遍历
代码
// 排序 基于给定的数组已排序
// Arrays.sort(nums1); Arrays.sort(nums2);
private static int[] func(int[] nums1, int[] nums2) {
int length1 = nums1.length, length2 = nums2.length;
int index1 = 0, index2 = 0, indexRes = 0;
// 存放结果的数组
int[] res = new int[length1 > length2 ? length1 : length2];
// 当有一个数组结束遍历, 则终止全部遍历
// 双指针, 判断指针指定的值大小, 来挪动指针位置
while (index1 < length1 && index2 < length2) {
if (nums1[index1] == nums2[index2]) {
res[indexRes++] = nums1[index1];
index1++;
index2++;
}
else if (nums1[index1] > nums2[index2]) {
index2++;
}
else index1++;
}
// 截取结果
return Arrays.copyOf(res, indexRes);
}
from pisecespeng.record.me.
LeetCode题解
解题思路
- 初始两个指针指向数组头部,
- 每次比较两指针指向的数组大小,
- 将较小数字的指针右移一位, 若两数相等则记录, 并两指针均右移一位,
- 当至少有一个指针超出范围, 遍历结束.
代码
public static int[] func(int[] nums1, int[] nums2) {
Arrays.sort(nums1);
Arrays.sort(nums2);
int length1 = nums1.length, length2 = nums2.length;
int[] intersection = new int[Math.min(length1, length2)];
int index1 = 0, index2 = 0, index = 0;
while (index1 < length1 && index2 < length2) {
if (nums1[index1] < nums2[index2]) {
index1++;
} else if (nums1[index1] > nums2[index2]) {
index2++;
} else {
intersection[index] = nums1[index1];
index1++;
index2++;
index++;
}
}
return Arrays.copyOfRange(intersection, 0, index);
}
from pisecespeng.record.me.
LeetCode题解
解题思路
- 先将第一个数组中的所有数字及其出现次数记录在Map中,
- 遍历第二个数组, 并与Map匹配, 得出交集.
代码
public static int[] func(int[] nums1, int[] nums2) {
// 可以通过遍历较短的数组, 来降低空间复杂度
if (nums1.length > nums2.length) {
return func(nums2, nums1);
}
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int num : nums1) {
int count = map.getOrDefault(num, 0) + 1;
map.put(num, count);
}
int[] intersection = new int[nums1.length];
int index = 0;
for (int num : nums2) {
int count = map.getOrDefault(num, 0);
if (count > 0) {
intersection[index++] = num;
count--;
if (count > 0) {
map.put(num, count);
} else {
map.remove(num);
}
}
}
return Arrays.copyOfRange(intersection, 0, index);
}
from pisecespeng.record.me.
Related Issues (20)
- 两数相加 HOT 2
- 买卖股票的最佳时机 HOT 3
- 买卖股票的最佳时期2 HOT 3
- 二叉树的层序遍历 HOT 2
- 二叉树的最大深度 HOT 2
- 二进制求和 HOT 4
- 从尾到头打印链表 HOT 2
- 位1的个数 HOT 4
- 删除排序数组中的重复项 HOT 2
- 删除链表的倒数第N个节点 HOT 3
- 加一 HOT 2
- 反转字符串 HOT 2
- 反转链表 HOT 3
- 只出现一次的数字 HOT 2
- 合并两个有序数组 HOT 3
- 合并两个有序链表 HOT 2
- 回文数 HOT 2
- 回文链表 HOT 3
- 复制带随机指针的链表 HOT 2
- 外观数列 HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pisecespeng.record.me.