leetcode
weixin_44029692
这个作者很懒,什么都没留下…
展开
-
一个有序数组,有重复的数,平方后,数组当中有多少不同的数字
一个有序数组,有重复的数,平方后,数组当中有多少不同的数字?例子,[-1,3,3],返回结果 2.例子,[-1,-1,1,1],返回结果 1.思路:使用双指针,对left和right的绝对值进行比较,如果不相等,那么绝对值大的一边进行收缩,并且不相等的数加一,但是要记录一下这个数的绝对值,这样碰到相同的可以越过去。比如[-1,3,3],一开始right绝对值为3,left绝对值为1,不相等,res为1,记录pre为3,右边向里面收缩一位,然后right绝对值还是3,left绝对值为1,发现right和原创 2020-07-01 10:16:02 · 545 阅读 · 0 评论 -
LeetCode378. Kth Smallest Element in a Sorted Matrix解答(值域二分法)
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix.Note that it is the kth smallest element in the sorted order, not the kth distinct element.Example:matrix = [[ 1, 5, 9]原创 2020-05-13 20:54:23 · 178 阅读 · 0 评论 -
LeetCode236. Lowest Common Ancestor of a Binary Tree解答 (递归)
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as de原创 2020-05-12 15:56:10 · 86 阅读 · 0 评论 -
LeetCode199. Binary Tree Right Side View解答(BFS+DFS)
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.For example:Given the following binary tree,Example:Input: [1,2,3,null,5,null,4]Output: [1, 3, 4]Explanation原创 2020-05-12 12:06:12 · 104 阅读 · 0 评论