在未排序的数组中找到第 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 ≤ 数组的长度。
备注:
使用PriorityQueue来构造最小堆,始终让堆中保存着遍历到当前的最小k个元素,遍历完成后,取出堆顶即可。也可以使用快排的partition函数
class Solution {
public int findKthLargest(int[] nums, int k) {
PriorityQueue<Integer> queue = new PriorityQueue<>((a, b) -> a - b);
for (int num : nums) {
if (queue.size() < k) {
queue.add(num);
} else {
if (num > queue.peek()) {
queue.poll();
queue.add(num);
}
}
}
return queue.peek();
}
}
请判断一个链表是否为回文链表。
示例 1:
输入: 1->2
输出: false
示例 2:
输入: 1->2->2->1
输出: true
进阶:
你能否用O(n)时间复杂度和O(1)空间复杂度解决此题?
class Solution(object):
def isPalindrome(self, head):
if head == None or head.next == None:
return True
fast = head
mid = head.next
stack = []
while mid:
#找中点
if fast.next and fast.next.next:
mid = mid.next
fast = fast.next.next
#右半区间入栈
else:
stack.append(mid.val)
mid = mid.next
while len(stack)!=0:
if stack.pop() != head.val:
return False
head = head.next
return True
给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”
备注:
参考Code Interview,不太懂这样为什么可以
class Solution(object):
def lowestCommonAncestor(self, root, p, q):
if root == None or root == p or root == q:
return root
left = self.lowestCommonAncestor(root.left, p, q)
right = self.lowestCommonAncestor(root.right, p, q)
return root if left and right else (left if left else right)
给定长度为 n 的整数数组 nums,其中 n > 1,返回输出数组 output ,其中 output[i] 等于 nums 中除 nums[i] 之外其余各元素的乘积。
示例:
输入: [1,2,3,4]
输出: [24,12,8,6]
说明: 请不要使用除法,且在 O(n) 时间复杂度内完成此题。
进阶:
你可以在常数空间复杂度内完成这个题目吗?( 出于对空间复杂度分析的目的,输出数组不被视为额外空间。)
使用两个数组记录从左的累乘和从右的累乘
class Solution(object):
def productExceptSelf(self, nums):
l = [nums[0]] * len(nums)
r = [nums[-1]] * len(nums)
for i in range(1,len(nums)):
l[i] = nums[i] * l[i-1]
r[-i-1] = nums[-i-1]*r[-i]
res = [0] * len(nums)
for i in range(1,len(nums)-1):
res[i] = l[i-1] * r[i+1]
res[0] = r[1]
res[-1] = l[-2]
return res
常数时间复杂度:
# int left = 1;
# int right = 1;
# int len = nums.length;
# int[] output = new int[len];
# for(int i=0;i<len;i++){
# output[i] = left;
# left *= nums[i];
# }
# for(int j=len-1;j>=0;j--){
# output[j] *= right;
# right *= nums[j];
# }
# return output;
# class Solution {
# // 从左至右乘一遍,从右到左乘一遍
# public int[] productExceptSelf(int[] nums) {
# int len = nums.length;
# int[] re = new int[len];
# if(len == 0) return new int[]{0};
# int help = 1;
# for(int i = 0; i < len; i++){
# re[i] = help;
# help *= nums[i];
# }
# help = 1;
# for(int i = len-1; i >= 0; i--){
# re[i] *= help;
# help *= nums[i];
# }
# return re;
# }
# class Solution:
# def productExceptSelf(self, nums: List[int]) -> List[int]:
# res = []
# for i in range(len(nums)):
# tmp = 1
# for x in nums[:i] + nums[i+1:]:
# tmp *= x
# res.append(tmp)
# return res
283.移动零
给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。
示例:
输入: [0,1,0,3,12]
输出: [1,3,12,0,0]
说明:
必须在原数组上操作,不能拷贝额外的数组。尽量减少操作次数。
备注:
也可以非零元素直接填充,剩余元素直接置0
class Solution(object):
def moveZeroes(self, nums):
count = 0
for i in range(len(nums)):
if nums[i] == 0:
count +=1
else:
nums[i-count] = nums[i]
if count > 0:
nums[i] = 0