Zheaoli / do-something-right

MIT License
37 stars 3 forks source link

2022-08-20 #335

Open Zheaoli opened 2 years ago

Zheaoli commented 2 years ago

2022-08-20

gongpeione commented 2 years ago
/*
 * @lc app=leetcode id=581 lang=typescript
 *
 * [581] Shortest Unsorted Continuous Subarray
 */

// @lc code=start
function findUnsortedSubarray(nums: number[]): number {
    const sorted = [...nums].sort((a, b) => a - b);

    let start = nums.length;
    let end = 0;
    for (let i = 0; i < nums.length; i++) {
        if (nums[i] === sorted[i]) continue;
        start = Math.min(start, i);
        end = Math.max(end, i);
    }

    return end - start > 0 ? end - start + 1 : 0;
};
// @lc code=end

微信id: 弘树 来自 vscode 插件

SaraadKun commented 2 years ago

654. 最大二叉树

struct TreeNode* construct(const int* nums, int lo, int hi) {
    if (lo == hi) {
        return NULL;
    }
    int max = lo;
    for (int i = lo + 1; i < hi; ++i) {
        if (nums[i] > nums[max]) {
            max = i;
        }
    }
    struct TreeNode* node = (struct TreeNode*)malloc(sizeof(struct TreeNode));
    node->val = nums[max];
    node->left = construct(nums, lo, max);
    node->right = construct(nums, max + 1, hi);
    return node;
}

struct TreeNode* constructMaximumBinaryTree(int* nums, int numsSize){
    return construct(nums, 0, numsSize);
}

WeChat: Saraad