z*****o 发帖数: 28 | 1 开始刷题,提交之后告诉Runtime: 116 ms,如何知道这个运行时间好坏?能否看到别
人的提交?看到有Java的Runtime 4ms,差很多,自己的算法好坏如何判断?
Given two arrays, write a function to compute their intersection.
Example:
Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2].
Note:
Each element in the result should appear as many times as it shows in both
arrays.
The result can be in any order.
难度Easy。
我的代码:
/**
* @param {number[]} nums1
* @param {number[]} nums2
* @return {number[]}
*/
var intersect = function(nums1, nums2) {
if ( nums1.length === 0 || nums2.length === 0) {
return [];
}
// compare numbers
var compare = function(num1, num2){
return parseInt(num1) - parseInt(num2);
}
// sort
nums1 = nums1.sort(compare);
nums2 = nums2.sort(compare);
// get intersaction
return intersaction(nums1, nums2);
};
function intersaction(nums1, nums2){
var p1 = 0;
var p2 = 0;
var result = [];
while (p1 < nums1.length && p2 < nums2.length) {
if (nums1[p1] === nums2[p2]) {
result.push(nums1[p1]);
p1++;
p2++;
} else if (nums1[p1] < nums2[p2]) {
p1++;
} else {
p2++;
}
}
return result;
} | e**y 发帖数: 784 | 2 点discuss,看most voted solution
https://leetcode.com/discuss/103345/three-java-solutions
给个包子吧 :)
【在 z*****o 的大作中提到】 : 开始刷题,提交之后告诉Runtime: 116 ms,如何知道这个运行时间好坏?能否看到别 : 人的提交?看到有Java的Runtime 4ms,差很多,自己的算法好坏如何判断? : Given two arrays, write a function to compute their intersection. : Example: : Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2]. : Note: : Each element in the result should appear as many times as it shows in both : arrays. : The result can be in any order. : 难度Easy。
|
|