1161. 最大层内元素和
dfs深搜,能比较方便的统计层数。
/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode(int x) { val x; }* }*/
class Solution {private int[] val…
给你一个二叉树,请你返回其按 层序遍历 得到的节点值。 (即逐层地,从左到右访问所有节点)。 /*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* T…
文章目录Design Front Middle Back Queue 设计前中后队列Description思路Design Front Middle Back Queue 设计前中后队列
Description
Design a queue that supports push and pop operations in the front, middle, and back.
Pushing 6 into the middle of [1, 2, 3, 4, …
队列queue函数There is a function queue::empty() that can be used to check whether queue is empty or not – it returns 1 (true) if queue is empty else returns 0 (false). 有一个函数queue :: empty()可用于检查队列是否为空–如果队列为空,则返回1(真)&…
//双向循环队列代码实现(C)
class MyCircularDeque {private:vector<int> arr;//head指向数组第一个数据,tail指向数组最后一个数据的下一个位置,count为数组元素个数int head,tail,count;
public:/** Initialize your data…
public class DepthFirstPaths {private boolean marked[];// 用于存放被标记过的顶点private int edgeTo[];// 数组内的元素表示某顶点,其索引表示某顶点的相邻顶点。private final int s;// 起点private int count;// 已遍历顶点的数量public DepthFirstPaths(Gra…
Java实现如下:
import java.util.*;
/**
public class TreeNode {int val 0;TreeNode left null;TreeNode right null;public TreeNode(int val) {this.val val;}
}
*/
public class Solution {public ArrayList<Integer> PrintFromTopToBottom(TreeNode…
994. 腐烂的橘子
洪泛法
class Solution {public int orangesRotting(int[][] grid) {int m grid.length;int n grid[0].length;int num 0; //新鲜橘子的个数Queue<Point> qu new LinkedList<>();for (int i 0; i < m; i) {for (int j 0; j < n; j) …
C —队列(queue)
今天我们来学习一下C中的队列(queue)。
特点
先进先出,后进后出。 什么意思呢? 队列只能出队最顶层,进队最底层。 比如: a(加入a) b a(加入b) c b a(加入c) c b(踢掉a) c(踢掉b) (踢掉c) 踢掉的顺序和加入的顺…
stl:queue 源码In C STL, Queue is a type of container that follows FIFO (First-in-First-Out) elements arrangement i.e. the elements which insert first will be removed first. In queue, elements are inserted at one end known as "back" and are delete…
文章目录Find K Pairs with Smallest Sums 查找和最小的K对数字思路TagFind K Pairs with Smallest Sums 查找和最小的K对数字
You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k.
Define a pair (u, v) which consists of one …
wait(long timeout)/notify()/notifyAll() 网上好多解释不是不全面就是有偏差,还是直接看官方解释
先来看一下源码:
public class Object {public final native void notify();public final native void notifyAll();public final native void wait(lo…
#图的深度优先和广度优先遍历
def DFS(graph,s): #深度优先遍历,基于栈stack[] #建立栈stack.append(s) data[] #记录已经遍历过的点data.append(s)while stack:nstack.pop() # 取出栈中最后一个元素并删掉nodesgraph[n]for i in nodes[::-1]: #栈先进后出if i not in data:st…
文章目录Kth Largest Element in a Stream 数据流中的第 K 大元素思路TagKth Largest Element in a Stream 数据流中的第 K 大元素
Design a class to find the kth largest element in a stream. Note that it is the kth largest element in the sorted order, not the kth …
public class BreadthFirstPaths {private boolean[] marked;// 保存被标记的顶点private int[] edgeTo;// 保存起点到与之连通的顶点之间的最短路径private final int s;// 起点public BreadthFirstPaths(Graph G, int s) {marked new boolean[G.V()];edgeTo new int[G.V()]…
题目: 用栈来模拟一个队列,要求实现队列的两个基本操作:入队、出队。 public class StackQueue {private Stack<Integer> stackA new Stack<>();private Stack<Integer> stackB new Stack<>();/*** 入队* param el…
原题链接
Alice likes snow a lot! Unfortunately, this year’s winter is already over, and she can’t expect to have any more of it. Bob has thus bought her a gift — a large snow maker. He plans to make some amount of snow every day. On day i he will make …