leetcode162】的更多相关文章

A peak element is an element that is greater than its neighbors. Given an input array nums, where nums[i] ≠ nums[i+1], find a peak element and return its index. The array may contain multiple peaks, in that case return the index to any one of the pea…
1题目 A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ num[i+1], find a peak element and return its index. The array may contain multiple peaks, in that case return the index to any one of the peaks i…
class Solution { public: int findPeakElement(vector<int>& nums) { int n = nums.size(); ) { ; } ] > nums[]) { ; } ] < nums[n - ]) { ; } ; i < nums.size() - ; i++) { ] && nums[i] > nums[i + ]) { return i; } } } };…
162.寻找峰值 描述 峰值元素是指其值大于左右相邻值的元素. 给定一个输入数组 nums,其中 nums[i] ≠ nums[i+1],找到峰值元素并返回其索引. 数组可能包含多个峰值,在这种情况下,返回任何一个峰值所在位置即可. 你可以假设 nums[-1] = nums[n] = -∞. 示例 示例 1: 输入: nums = [1,2,3,1] 输出: 2 解释: 3 是峰值元素,你的函数应该返回其索引 2. 示例 2: 输入: nums = [1,2,1,3,5,6,4] 输出: 1…
题目描述: 方法:穷举暴力 class Solution: def maxScoreWords(self, words: List[str], letters: List[str], score: List[int]) -> int: from collections import Counter n = len(words) v = lambda c: ord(c) - ord('a') words = [Counter(v(c) for c in w) for w in words] sco…
题目描述: 自己的提交: class Solution: def closedIsland(self, grid: List[List[int]]) -> int: def dfs(grid,r,c): nr = len(grid) nc = len(grid[0]) if r<0 or c<0 or r==nr or c==nc or grid[r][c]==1: return grid[r][c] = 1 dfs(grid,r-1,c) dfs(grid,r+1,c) dfs(gri…
题目描述: 自己的提交: class Solution: def reconstructMatrix(self, upper: int, lower: int, colsum: List[int]) -> List[List[int]]: res = [[],[]] for v,i in enumerate(colsum): if i > 2: return [] elif i == 2: res[0].append(1) res[1].append(1) upper,lower = uppe…
题目描述: 自己的提交: class Solution: def oddCells(self, n: int, m: int, indices: List[List[int]]) -> int: nums = [[0] * m for _ in range(n)] for i,j in indices: for k in range(m): nums[i][k] += 1 for k in range(n): nums[k][j] += 1 res = 0 for i in range(n):…
示例 2: 输入: nums = [1,2,1,3,5,6,4] 输出: 1 或 5 解释: 你的函数可以返回索引 1,其峰值元素为 2:   或者返回索引 5, 其峰值元素为 6. 说明: 你的解法应该是 O(logN) 时间复杂度的. 二分法: 如果中间的是峰值直接返回,如果不是,那么两边较大的那一侧是存在峰值的. class Solution { public: int findPeakElement(vector<int>& nums) { int len = nums.siz…
二分法相关 153. Find Minimum in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). Find the minimum element. You may assume no duplicate exists in the array. (M…
快手面试准备 我的牛客网帖子链接:https://www.nowcoder.com/discuss/429362 一面: 基础知识 1.java基本数据类型(8种) 1.基本数据类型有哪些,各占多少位,浮点型表示能否精确的表示小数,int的范围(这个当时脑袋可能抽了,顺嘴说了2-32到232 - 1,面试官可能知道我的意思,没有指出)最大值为什么要减一,int的最小值在计算机中怎么表示(我回答的是二进制表示,没有思考直接说是32位全是1,回答错了,实际是10000....0000) 这个知识点很…