leetcode Day20----array.easy

这篇博客主要讨论了LeetCode中的三个问题:如何使数组非递减,找到最长连续递增子序列,以及确定数组的度。通过C语言和Python3的实现,展示了如何解决这些问题,同时提供了运行时间和内存使用情况。

Non-decreasing Array

Given an array with n integers, your task is to check if it could become non-decreasing by modifying at most 1 element.

We define an array is non-decreasing if array[i] <= array[i + 1] holds for every i (1 <= i < n).

Example 1:
Input: [4,2,3]
Output: True
Explanation: You could modify the first 4 to 1 to get a non-decreasing array.
Example 2:
Input: [4,2,1]
Output: False
Explanation: You can’t get a non-decreasing array by modify at most one element.
Note: The n belongs to [1, 10,000].

C语言

bool checkNonDes(int* nums, int numsSize, int* i) {
    for (int j = 0; j+1 < numsSize; j++) {
        if (nums[j] > nums[j+1]) {
            *i = j;
            return false;
        }
    }
    return true;
}


bool checkPossibility(int* nums, int numsSize){
    int i;
    if (numsSize < 3)
        return true;
    if (checkNonDes(nums,numsSize,&i) == false){
        if (i < numsSize-2) {
            if (nums[i] < nums[i+2]) {
                nums[i+1] = nums[i];
                if (checkNonDes(nums,numsSize,&i) == false){
                    return false;
                }
            } else {
                nums[i] = nums[i+1];
                if (checkNonDes(nums,numsSize,&i) == false){
                    return false;
                }
            }
        }
    }
    return true;
}


Success
Details
Runtime: 28 ms, faster than 12.56% of C online submissions for Non-decreasing Array.
Memory Usage: 8.3 MB, less than 100.00% of C online submissions for Non-decreasing Array.

python3

class Solution:
    def checkPossibility(self, nums: List[int]) -> bool:
        count_dec = 0
        for i in range(len(nums) - 1):
            if nums[i] > nums[i + 1]:
                count_dec += 1
                if i == 0:
                    nums[i] = nums[i + 1]
                elif nums[i - 1] <= nums[i + 1]:
                    nums[i] = nums[i - 1]
                else:
                    nums[i + 1] = nums[i]
            if count_dec > 1:
                return False
        return True

Success
Details
Runtime: 48 ms, faster than 99.86% of Python3 online submissions for Non-decreasing Array.
Memory Usage: 14.2 MB, less than 5.95% of Python3 online submissions for Non-decreasing Array.

Longest Continuous Increasing Subsequence

Given an unsorted array of integers, find the length of longest continuous increasing subsequence (subarray).

Example 1:
Input: [1,3,5,4,7]
Output: 3
Explanation: The longest continuous increasing subsequence is [1,3,5], its length is 3.
Even though [1,3,5,7] is also an increasing subsequence, it’s not a continuous one where 5 and 7 are separated by 4.
Example 2:
Input: [2,2,2,2,2]
Output: 1
Explanation: The longest continuous increasing subsequence is [2], its length is 1.
Note: Length of the array will not exceed 10,000.

C语言



int findLengthOfLCIS(int* nums, int numsSize){
    int i=0,count=0,res=0;
    if(numsSize<1)return 0;
    while(i<numsSize-1){if(nums[i]<nums[i+1]){count++;if(count>res)res=count;}else count=0;i++;}
    return res+1;
}


Success
Details
Runtime: 8 ms, faster than 42.35% of C online submissions for Longest Continuous Increasing Subsequence.
Memory Usage: 7.5 MB, less than 100.00% of C online submissions for Longest Continuous Increasing Subsequence.

python3

class Solution:
    def findLengthOfLCIS(self, nums: List[int]) -> int:
        if not nums:
            return 0
        res = 1
        c = 1
        for i in range(1, len(nums)):
            c = c + 1 if nums[i] > nums[i-1] else 1
            res = max(res, c)
        return res

Success
Details
Runtime: 48 ms, faster than 48.89% of Python3 online submissions for Longest Continuous Increasing Subsequence.
Memory Usage: 14.1 MB, less than 6.67% of Python3 online submissions for Longest Continuous Increasing Subsequence.

Degree of an Array

Given a non-empty array of non-negative integers nums, the degree of this array is defined as the maximum frequency of any one of its elements.

Your task is to find the smallest possible length of a (contiguous) subarray of nums, that has the same degree as nums.

Example 1:
Input: [1, 2, 2, 3, 1]
Output: 2
Explanation:
The input array has a degree of 2 because both elements 1 and 2 appear twice.
Of the subarrays that have the same degree:
[1, 2, 2, 3, 1], [1, 2, 2, 3], [2, 2, 3, 1], [1, 2, 2], [2, 2, 3], [2, 2]
The shortest length is 2. So return 2.
Example 2:
Input: [1,2,2,3,1,4,2]
Output: 6
Note:

nums.length will be between 1 and 50,000.
nums[i] will be an integer between 0 and 49,999.

C语言

int findShortestSubArray(int* nums, int numsSize){
    int count[50000]={0};
    int first[50000]={0};
    int res=0,degree=0;
    for(int i=0;i<numsSize;i++){
        if(first[nums[i]]==0)
            first[nums[i]]=i+1;
        count[nums[i]]++;
        if(count[nums[i]]>degree){
            degree=count[nums[i]];
            res=i-first[nums[i]]+2;
        }
        else if(count[nums[i]]==degree){
            if((i-first[nums[i]]+2)<res)
                res=i-first[nums[i]]+2;
        }
    }
    return res;
}

Success
Details
Runtime: 28 ms, faster than 40.54% of C online submissions for Degree of an Array.
Memory Usage: 9.3 MB, less than 100.00% of C online submissions for Degree of an Array.

python3

class Solution:
    def findShortestSubArray(self, nums: List[int]) -> int:
        dic = {}
        for i in nums:
            if i in dic:
                dic[i] += 1
            else:
                dic[i] = 1
        degree = max(dic.values())
        if degree == 1:
            return 1
        max_vec = []
        for i in dic:
            if dic[i] == degree:
                max_vec.append(i)
                
        shortest_len = float('inf')
        for i in range(len(max_vec)):
            temp = len(nums) - nums.index(max_vec[i]) - nums[::-1].index(max_vec[i])
            shortest_len = min(shortest_len,temp)
        return shortest_len

Success
Details
Runtime: 60 ms, faster than 96.88% of Python3 online submissions for Degree of an Array.
Memory Usage: 14.5 MB, less than 14.06% of Python3 online submissions for Degree of an Array.

<think>好的,我现在需要帮用户解决一个Node.js中的TypeError问题,错误信息是“TypeError: cb is nota function”。用户提供的错误堆栈显示这个问题发生在parseCookie函数中,具体是在调用cb的时候。首先,我得分析可能的原因。 首先,错误信息指出cb不是一个函数。这通常意味着在调用某个函数时,传入的回调参数不是函数类型。可能的情况包括: 1. **回调未正确传递**:可能在调用parseCookie时,没有正确传递回调函数,或者传递了错误的参数类型。 2.**函数定义参数不匹配**:parseCookie函数可能期望一个回调函数作为参数,但在调用时可能没有传入或者传入了其他类型的值。 3. **异步处理问题**:可能使用了错误的回调方式,比如应该使用函数的地方用了Promise或其他异步处理方式。 4. **第三方库版本问题**:可能使用的库版本过旧或存在bug,导致回调参数处理异常。 接下来,我需要根据用户提供的堆栈信息来定位具体的位置。错误发生在leetcode.js的parseCookie函数第542行,调用cb的地方。这可能是一个插件或模块中的函数,用户可能在使用VSCode的LeetCode插件时遇到了问题,尤其是在处理cookie登录时。 考虑到用户提到的环境是Node.js v22.17.0,可能存在兼容性问题。某些旧版本的库可能不兼容较新的Node.js版本,导致回调参数未被正确处理。 用户可能需要检查以下几点: -检查parseCookie函数的定义,确认其参数是否正确要求一个回调函数。 -查看调用parseCookie的地方是否正确传递了回调函数。 -确认第三方库(如vsc-leetcode-cli)是否支持当前Node.js版本,可能需要升级或降级版本。- 查看是否有相关的Issue或更新日志,可能其他用户也遇到了类似的问题。 可能的解决方案包括:1. 更新LeetCode插件到最新版本,修复可能存在的bug。2. 降级Node.js版本到更稳定的版本,比如LTS版本,以确保兼容性。 3. 如果问题由特定代码引起,检查代码中回调函数的使用是否正确。 4.清除缓存或重新安装插件,解决可能的配置错误。 此外,用户可能需要提供更多的上下文信息,比如他们正在执行的操作,或者相关的代码片段,以便更准确地定位问题。例如,是否在自定义代码中调用了parseCookie函数,或者这完全是由插件内部触发的错误。 总结来说,用户需要检查回调函数的传递和定义,确认第三方库的兼容性,并考虑环境配置是否合适。如果问题持续存在,建议查看插件的官方文档或提交Issue寻求帮助。</think>`TypeError: cb is not a function` 表示在调用 `parseCookie` 函数时,传递的 `cb` 参数 **不是函数类型**。以下是关键分析及解决方案: --- ### ❌ 错误原因 1. **回调参数类型错误** `parseCookie` 函数内部尝试调用 `cb(...)`,但传入的 `cb` 可能是 `null`、`undefined` 或其他非函数类型(如字符串、数字)。 2. **函数定义与调用不匹配** - 函数定义时要求传入回调函数: ```javascript function parseCookie(cookieStr, cb) { ... } ``` - 调用时却未传递有效回调: ```javascript parseCookie("key=value", "错误参数"); // 第二个参数不是函数 ``` 3. **第三方库兼容性问题** 根据路径 `vsc-leetcode-cli` 和 Node.js v22.17.0,可能是 **LeetCode 插件与最新 Node.js 版本不兼容**,导致内部回调参数传递异常。 --- ### ✅ 解决方案 1. **检查调用方代码** 定位 `parseCookie` 的调用位置(如 `leetcode.js` 第 565 行),确保传递正确的回调函数: ```javascript parseCookie("key=value", (err, result) => { if (err) throw err; console.log(result); }); ``` 2. **更新插件版本** `vsc-leetcode-cli` 可能存在旧版 bug,尝试更新插件: ```bash npm install -g vsc-leetcode-cli@latest ``` 或重新安装 VSCode LeetCode 插件。 3. **降级 Node.js 版本** Node.js v22.x 是较新版本,可能存在兼容性问题。建议切换到 **LTS 版本(如 v18.x 或 v20.x)**- 使用 [nvm](https://github.com/nvm-sh/nvm) 管理多版本 Node.js。 4. **临时修复(绕过错误)** 在调用 `parseCookie` 前增加类型检查: ```javascript if (typeof cb === 'function') { cb(null, parsedCookie); } else { console.error("回调参数必须为函数"); } ``` --- ### 🧪 验证步骤 1. 确认 Node.js 版本: ```bash node -v # 推荐使用 v18.x 或 v20.x ``` 2. 检查插件依赖: 进入插件目录并更新依赖: ```bash cd ~/.vscode-huawei/extensions/leetcode.vscode-leetcode-0.18.1 npm install ``` --- ### ❓ 常见相关问题
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值