原文链接: https://leetcode-cn.com/problems/minimum-height-tree-lcci 英文原文Given a sorted (increasing order) array with unique integer elements, write an algo­rithm to create a binary search tree with minimal height. Example: Given sorted array: [-10,-3…
标签详情
分治 分类
原文链接: https://leetcode-cn.com/problems/er-wei-shu-zu-zhong-de-cha-zhao-lcof 中文题目在一个 n * m 的二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个高效的函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。 示例: 现有矩阵 matrix 如下: [ [1, 4, 7, 11, 15], [2, 5, 8, 12, 19], [3, 6, 9, 16, 2…
原文链接: https://leetcode-cn.com/problems/zhong-jian-er-cha-shu-lcof 中文题目输入某二叉树的前序遍历和中序遍历的结果,请构建该二叉树并返回其根节点。 假设输入的前序遍历和中序遍历的结果中都不含重复的数字。 示例 1: Input: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7] Output: [3,9,20,null,null,15,7] 示例 2: Input: preorder…
原文链接: https://leetcode-cn.com/problems/zui-xiao-de-kge-shu-lcof 中文题目输入整数数组 arr ,找出其中最小的 k 个数。例如,输入4、5、1、6、2、7、3、8这8个数字,则最小的4个数字是1、2、3、4。 示例 1: 输入:arr = [3,2,1], k = 2 输出:[1,2] 或者 [2,1] 示例 2: 输入:arr = [0,1,2,1], k = 1 输出:[0] 限制: 0 <= k <= arr.length <…
原文链接: https://leetcode-cn.com/problems/lian-xu-zi-shu-zu-de-zui-da-he-lcof 中文题目输入一个整型数组,数组中的一个或连续多个整数组成一个子数组。求所有子数组的和的最大值。 要求时间复杂度为O(n)。 示例1: 输入: nums = [-2,1,-3,4,-1,2,1,-5,4] 输出: 6 解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。 提示: 1 <= arr.length <= 10^5 -100 <= …
原文链接: https://leetcode-cn.com/problems/shu-zu-zhong-chu-xian-ci-shu-chao-guo-yi-ban-de-shu-zi-lcof 中文题目数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。 你可以假设数组是非空的,并且给定的数组总是存在多数元素。 示例 1: 输入: [1, 2, 3, 2, 2, 2, 5, 4, 2] 输出: 2 限制: 1 <= 数组长度 <= 50000 注意:本题与主站 169 题相同:ht…
原文链接: https://leetcode-cn.com/problems/shu-zu-zhong-de-ni-xu-dui-lcof 中文题目在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组中的逆序对的总数。 示例 1: 输入: [7,5,6,4] 输出: 5 限制: 0 <= 数组长度 <= 50000 通过代码 高赞题解📺 视频题解 📖 文字题解方法一:归并排序预备知识 「归并排序」是分治思想的典型应用,它包含这样三个步骤:…
原文链接: https://leetcode-cn.com/problems/contiguous-sequence-lcci 英文原文You are given an array of integers (both positive and negative). Find the contiguous sequence with the largest sum. Return the sum. Example: Input: [-2,1,-3,4,-1,2,1,-5,4] Output: 6 …
原文链接: https://leetcode-cn.com/problems/sorted-matrix-search-lcci 英文原文Given an M x N matrix in which each row and each column is sorted in ascending order, write a method to find an element. Example: Given matrix: [ [1, 4, 7, 11, 15], [2, 5, 8, 12, 19…
原文链接: https://leetcode-cn.com/problems/smallest-k-lcci 英文原文Design an algorithm to find the smallest K numbers in an array. Example: Input: arr = [1,3,5,7,2,4,6,8], k = 4 Output: [1,2,3,4] Note: 0 <= len(arr) <= 100000 0 <= k <= min(100000, len(arr)) …
原文链接: https://leetcode-cn.com/problems/g5c51o 中文题目给定一个整数数组 nums 和一个整数 k ,请返回其中出现频率前 k 高的元素。可以按 任意顺序 返回答案。 示例 1: 输入: nums = [1,1,1,2,2,3], k = 2 输出: [1,2] 示例 2: 输入: nums = [1], k = 1 输出: [1] 提示: 1 <= nums.length <= 105 k 的取值范围是 [1, 数组中不相同的元素的个数] 题目数据保…
原文链接: https://leetcode-cn.com/problems/xx4gT2 中文题目给定整数数组 nums 和整数 k,请返回数组中第 k 个最大的元素。 请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素。 示例 1: 输入: [3,2,1,5,6,4] 和 k = 2 输出: 5 示例 2: 输入: [3,2,3,1,2,4,5,5,6] 和 k = 4 输出: 4 提示: 1 <= k <= nums.length <= 104 -104 <=…
原文链接: https://leetcode-cn.com/problems/7WHec2 中文题目给定链表的头结点 head ,请将其按 升序 排列并返回 排序后的链表 。 示例 1: 输入:head = [4,2,1,3] 输出:[1,2,3,4] 示例 2: 输入:head = [-1,5,3,4,0] 输出:[-1,0,3,4,5] 示例 3: 输入:head = [] 输出:[] 提示: 链表中节点的数目在范围 [0, 5 * 104] 内 -105 <= Node.val <= 10…
原文链接: https://leetcode-cn.com/problems/vvXgSW 中文题目给定一个链表数组,每个链表都已经按升序排列。 请将所有链表合并到一个升序链表中,返回合并后的链表。 示例 1: 输入:lists = [[1,4,5],[1,3,4],[2,6]] 输出:[1,1,2,3,4,4,5,6] 解释:链表数组如下: [ 1->4->5, 1->3->4, 2->6 ] 将它们合并到一个有序链表中得到。 1->1->2->3->4->4->5->6 示例 2: 输入:l…
上一页
2 / 2