LeetCode初级算法-数组-6
2019-01-29 12:58
295 查看
题目:两个数组的交集 II
C语言解题
int* intersect(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize) { int min,temp; *returnSize = 0; if (nums1Size < nums2Size) min = nums1Size; else min = nums2Size; int* re = (int*)malloc(sizeof(int)*min); for (int i = 0; i < nums1Size; i++) { for (int j = 0; j < nums2Size; j++) { if (nums1[i] == nums2[j]) { re[(*returnSize)] = nums2[j]; (*returnSize)++; temp = nums2[j]; nums2[j] = nums2[nums2Size - 1]; nums2[nums2Size - 1] = temp; nums2Size--; break; } } } return re; }
第二个数组的元素与第一个数组的元素比较,如果一样,就添加到re中,第二个数组的该元素移到最后,数组长度-1(避免重复比较)。
相关文章推荐
- LeetCode-探索-初级算法-数组-从排序数组中删除重复项-java
- LeetCode(初级算法)数组篇--存在重复c++
- LeetCode初级算法-数组-10
- LeetCode(初级算法)数组篇--只出现一次的数字c++
- LeetCode初级算法-数组-11
- leetcode 初级算法数组8 移动0
- LeetCode(初级算法)数组篇--两个数组的交集II c++
- LeetCode初级算法-数组-8
- Leetcode初级算法6两个数组的交集
- LeetCode初级算法-数组-9
- LeetCode初级算法数组9 两数之和
- LeetCode初级算法-数组-旋转数组(python)
- LeetCode探索模块初级算法数组章节python3代码实现
- LeetCode(初级算法)数组篇-移动零
- leetcode初级算法数组11 旋转图像
- LeetCode(初级算法)数组篇--从排序数组中删除重复项c++
- LeetCode(初级算法)数组篇--买卖股票的最佳时机 II c++
- leetcode 初级算法数组7加一
- LeetCode(初级算法)数组篇--旋转数组c++
- LeetCode初级算法-数组-7