数组中的第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 说明: 你可以假设 k 总是有效的,且 1 ≤ k ≤ 数组的长度. class Solution { public int findKthLargest(int[] nums, int…
215. 数组中的第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 说明: 你可以假设 k 总是有效的,且 1 ≤ k ≤ 数组的长度. class Solution { public int findKthLargest(int[] nums…
421. 数组中两个数的最大异或值 421. Maximum XOR of Two Numbers in an Array 题目描述 给定一个非空数组,数组中元素为 a0, a1, a2, - , an-1,其中 0 ≤ ai < 231. 找到 ai 和 aj 最大的异或 (XOR) 运算结果,其中 0 ≤ i,j < n. 你能在 O(n) 的时间解决这个问题吗? 每日一算法2019/7/13Day 71LeetCode421. Maximum XOR of Two Numbers in…
442. 数组中重复的数据 442. Find All Duplicates in an Array 题目描述 Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements that appear twice in this array. Could you do it without ex…
15. 3Sum:三数之和为0的组合 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: The solution set must not contain duplicate triplets. For example, gi…
方法有很多种 第一:直接循环,判断输出 第二:使用indexOf 正常来说,为了增加工作效率一般会选择indexOf,但是indexOf存在兼容性问题,因此最完善的写法如下 function indexOf(arr, item) { if (Array.prototype.indexOf){ //判断当前浏览器是否支持 return arr.indexOf(item);//支持,则直接使用indexOf函数进行输出 } else { for (var i = 0; i < arr.length;…
1. 题目 2. 解答 2.1 方法一 left 数组表示当前元素左边比当前元素小的元素个数,right 数组数组表示当前元素右边比当前元素小的元素个数.在山脉的中间 B[i] 处,其左边和右边肯定都有小于 B[i] 的元素,而山脉的长度即为 left[i] + right[i] + 1. class Solution { public: int longestMountain(vector<int>& A) { int n = A.size(); if (n < 3) retu…