最近我遇到了functools.cache,不知道它与functools.lru_cache.
我找到了关于和之间的区别的帖子,但没有专门针对和 的帖子。functools.cached_propertylru_cachecachelru_cache
有这个问题要求返回数组元素的所有唯一三元组,这些元素加起来为零(交换三元组中两个元素的位置不算作唯一)。
我想出了以下代码:
function threeSum(nums) {
nums.sort((a, b) => a - b);
const result = [];
for (let i = 0; i < nums.length; i++) {
// skipping duplicates
if (i !== 0 && nums[i] === nums[i - 1]) continue;
let left = i + 1;
let right = nums.length - 1;
while (left < right) {
const s = nums[i] + nums[left] + nums[right];
// too small; move to the right
if (s < 0) left++;
// too big; move …Run Code Online (Sandbox Code Playgroud)javascript algorithm computer-science time-complexity space-complexity