365. 水壶问题
class Solution {
public boolean canMeasureWater(int x, int y, int z) {
if(z == 0) return true;
if((x==0 && y != z)|| (y==0 && x != z)) return false;
if(x + y < z) return false;
y = gcd(x,y);
return z % y == 0;
}
private int gcd(int x,int y){
return x%y==0?y:gcd(y,x%y);
}
}
class Solution {
public boolean canMeasureWater(int x, int y, int z) {
if(z == 0) return true;
if((x==0 && y != z)|| (y==0 && x != z)) return false;
if(x + y < z) return false;
int temp;
while(x % y != 0){
temp = x % y;
x = y;
y = temp;
}
return z % y == 0;
}
}
//BFS
class Solution {
public boolean canMeasureWater(int x, int y, int z) {
if (z < 0 || z > x + y) {
return false;
}
Set<Integer> set = new HashSet<>();
Queue<Integer> q = new LinkedList<>();
q.offer(0);
while (!q.isEmpty()) {
int n = q.poll();
if (n + x <= x + y && set.add(n + x)) {
q.offer(n + x);
}
if (n + y <= x + y && set.add(n + y)) {
q.offer(n + y);
}
if (n - x >= 0 && set.add(n - x)) {
q.offer(n - x);
}
if (n - y >= 0 && set.add(n - y)) {
q.offer(n - y);
}
if (set.contains(z)) {
return true;
}
}
return false;
}
}
面试题54. 二叉搜索树的第k大节点
//递归
class Solution {
public int kthLargest(TreeNode root, int k) {
List<Integer> list = new ArrayList<>();
helper(root,list);
return list.get(list.size()-k);
}
public void helper(TreeNode root,List<Integer> list){
if(root==null) return;
if(root.left != null) helper(root.left,list);
list.add(root.val);
if(root.right != null) helper(root.right,list);
}
}
//递归(推荐)
class Solution {
private int ans = 0, count = 0;
public int kthLargest(TreeNode root, int k) {
helper(root, k);
return ans;
}
private void helper(TreeNode root, int k) {
if (root.right != null) helper(root.right, k);
if (++count == k) {
ans = root.val;
return;
}
if (root.left != null) helper(root.left, k);
}
}
//迭代
class Solution {
public int kthLargest(TreeNode root, int k) {
int count = 1;
Stack<TreeNode> stack = new Stack<>();
while (Objects.nonNull(root) || !stack.empty()) {
while (Objects.nonNull(root)) {
stack.push(root);
root = root.right;
}
TreeNode pop = stack.pop();
if (count == k) {
return pop.val;
}
count++;
root = pop.left;
}
return 0;
}
}
面试题32 - II. 从上到下打印二叉树 II
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> list = new ArrayList<>();
Queue<TreeNode> queue = new LinkedList<>();
if(root!=null) queue.add(root);
while(!queue.isEmpty()){
int size = queue.size();
List<Integer> res = new ArrayList<>();
while(size>0){
TreeNode node = queue.poll();
res.add(node.val);
if(node.left!=null){
queue.add(node.left);
}
if(node.right!=null){
queue.add(node.right);
}
size--;
}
list.add(res);
}
return list;
}
}
//Deque 双端队列
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> res = new LinkedList<>();
Deque<TreeNode> deque = new LinkedList<>();
if (root != null) {
deque.offer(root);
}
TreeNode flag = root;
TreeNode node;
List<Integer> row = new LinkedList<>();
while (!deque.isEmpty()) {
node = deque.poll();
row.add(node.val);
if (node.left != null) {
deque.offer(node.left);
}
if (node.right != null) {
deque.offer(node.right);
}
if (flag == node) {
flag = deque.peekLast();
res.add(row);
row = new LinkedList<>();
}
}
return res;
}
}
//递归
class Solution {
private List<List<Integer>> list =new ArrayList<>();
public List<List<Integer>> levelOrder(TreeNode root) {
helper(root,0);
return list;
}
private void helper(TreeNode node,int index){//index记录树的深度
if (node==null)
return;
if (index==list.size()) {//如果深度超出了list的size,就说明到了最新的深度
List<Integer> mid = new ArrayList<>();
mid.add(node.val);
list.add(mid);
}
else{//若没超出,则添加到已有的深度
List<Integer> mid = list.get(index);
mid.add(node.val);
list.set(index,mid);
}
helper(node.left,index+1);//继续递归
helper(node.right,index+1);
}
}
你知道的越多,你不知道的越多。
有道无术,术尚可求,有术无道,止于术。
如有其它问题,欢迎大家留言,我们一起讨论,一起学习,一起进步