- 博客(60)
- 资源 (1)
- 收藏
- 关注
原创 2021-09-25
确定dp数组(dp table)以及下标的含义 确定递推公式 dp数组如何初始化 确定遍历顺序 举例推导dp数组斐波那契数列class Solution { public int fib(int n) { if(n==0||n==1) return n; int sum; int m=0,k=1; for(int i=0;i<n;i++) { ...
2021-09-25 21:06:59
105
原创 2021-09-07
平衡二叉树class Solution { public boolean isBalanced(TreeNode root) { if(root==null) return true; if(Math.abs(cal(root.left)-cal(root.right))>1) { return false; } return isBalanced(root.left)&&
2021-09-07 15:39:32
105
原创 2021-09-06
二叉树的前序遍历class Solution { List<Integer> x=new LinkedList<>(); public List<Integer> preorderTraversal(TreeNode root) { if(root==null) return x; x.add(root.val); preorderTraversal(root.left); preorder
2021-09-06 14:11:46
132
原创 2021-09-05
求多叉树最大深度class Solution { public int maxDepth(Node root) { if(root==null) return 0; int y=0; for(Node x : root.children) { y=Math.max(maxDepth(x),y); } return y+1; }}二叉树的坡度class Solutio
2021-09-05 17:42:43
98
原创 2021-09-04
中序先序构造二叉树class Solution { public TreeNode buildTree(int[] preorder, int[] inorder) { if (preorder == null || inorder == null || preorder.length != inorder.length || inorder.length < 1) return null; return construct(preor
2021-09-04 17:05:08
86
原创 2021.9.3刷题
求根节点到叶节点数字之和class Solution { static int sum; public int sumNumbers(TreeNode root) { sum=0; Sum(0,root); return sum; } public static void Sum(int val,TreeNode root) { if(root==null) return; int k=
2021-09-03 21:05:09
96
原创 2121.9.2刷题
二叉树的最小深度class Solution { public int minDepth(TreeNode root) { int ans=1000000; if(root==null) return 0; if(root.left==null) { return minDepth(root.right)+1; } if(root.right==null) {.
2021-09-02 21:13:23
84
原创 图的操作最短路径最小生成树问题
import java.util.*;import java.util.Queue;import java.util.Stack;//有权无向图public class Graph { public Vertex[] vertexList; //存放顶点 public int[][] mGraph; //用矩阵表示边 public int size; //当前顶点数 public int[] distance; //
2021-09-01 17:51:17
108
原创 树的遍历插入最小平衡子树
import javax.swing.tree.TreeNode;import java.util.*;import java.util.Queue;import java.util.Stack;public class BinaryTreeNode { public Tree construct(int[] preorder, int[] inorder){ // 输入的合法性判断 if (preorder == null || inorder == nu
2021-09-01 15:00:41
132
原创 青蛙跳台阶和斐波那契数列
青蛙跳台阶class Solution {public: int numWays(int n) { int a = 1, b = 1, sum; for(int i = 0; i < n; i++){ sum = (a + b) % 1000000007; a = b; b = sum; } return a; }};斐波那契数列class
2021-08-16 11:48:03
96
原创 输入某二叉树的前序遍历和中序遍历的结果,请构建该二叉树并返回其根节点。
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: vector<int> Preorder ;
2021-08-16 11:39:39
605
原创 反转数组四种方式
https://blog.youkuaiyun.com/pro1515151515/article/details/90383807
2021-08-16 10:19:19
123
原创 2021-07-24
class Solution {public: vector<int> intersect(vector<int>& nums1, vector<int>& nums2) { vector<int> x; int j; sort(nums1.begin(),nums1.end()); //排序预处理 sort(nums2.begin(),nums2.end());
2021-07-24 19:13:08
87
原创 层次遍历存储
class Solution {public: vector<int> levelOrder(TreeNode* root) { if(!root) return {}; //如果节点为空放回空容器 queue<TreeNode*> data; //定义一个队列 TreeNode* p=root; //新增一个节点将头节点赋值给他 TreeNode* s; vector<in
2021-07-23 17:01:49
99
原创 2021-07-23
class Solution {public: bool validateStackSequences(vector<int>& pushed, vector<int>& popped) { stack<int> data; if(pushed.size() == 0 && popped.size() == 0){ return true; }
2021-07-23 16:13:34
70
原创 转圈循环数组
class Solution {public: vector<int> spiralOrder(vector<vector<int>>& matrix) { vector <int> x; if(matrix.empty()) return x; int top=0; int rh=matrix[0].size()-1; int rl=0; int
2021-07-23 15:36:09
126
原创 2021-07-23
class Solution {public: bool isSymmetric(TreeNode* root) { if(!root) return true; return Helper(root->left, root->right); } bool Helper(TreeNode* left, TreeNode* right) { if(!left && !right) {
2021-07-23 14:55:08
74
原创 镜像树xxx
TreeNode* mirrorTree(TreeNode* root) { if(!root) return root; TreeNode* tmp = root->left; root->left = root->right; root->right = tmp; mirrorTree(root->left); mirrorTree(root->right); .
2021-07-23 14:32:14
75
原创 2021-07-23
class Solution {public: int findRepeatNumber(vector<int>& nums) { unordered_set<int> c1; //定义哈希表存储数组 for(int num : nums) { if(c1.find(num)!=c1.end()) //利用迭代器判断是否存在这个元素 { return num;
2021-07-23 14:15:29
80
原创 2021-05-08
Android Studio中使用webview加载URL出现net::ERR_CLEARTEXT_NOT_PERMITTED cleartext为明文 permitted表示允许 即加载http时明文不被允许从Android 9.0(API级别28)开始,默认情况下禁用明文支持。因此http的url均无法在webview中加载因此需要添加允许权限<applicationandroid:allowBackup=“true”android:icon="@mipmap/ic_launcher"
2021-05-08 11:20:12
162
3
原创 不同的二叉搜索树
class Solution {public: int numTrees(int n) { vector<int> G(n + 1, 0); G[0] = 1; G[1] = 1; for (int i = 2; i <= n; ++i) { for (int j = 1; j <= i; ++j) { G[i] += G[j - 1] * G[i -
2021-04-24 20:46:00
44
原创 树的最小深度
class Solution {public: int minDepth(TreeNode* root) { if(root==NULL) { return 0; } int left = minDepth(root->left); int right = minDepth(root->right); if(left == 0) return right + 1;
2021-04-24 20:31:20
50
原创 前序遍历
class Solution {public: void visit(TreeNode* t,vector<int> &a) { if(t==NULL) { return; } a.push_back(t->val); visit(t->left,a); visit(t->right,a); } vector<in
2021-04-24 20:22:45
58
原创 平衡二叉树
class Solution{public: int dep(TreeNode* t) { if(t==NULL) { return 0; } else{ int l=dep(t->left); int r=dep(t->right); if(l>r) return l+1; else return r+1;;
2021-04-24 20:15:36
42
原创 2021-04-24
class Solution {public: int dep(TreeNode* t) { if(t==NULL) { return 0; } else { int l=maxDepth(t->left); int r=maxDepth(t->right); if(l>r) return l+1
2021-04-24 19:31:25
47
原创 对称二叉树
class Solution {public: void check(TreeNode* t,TreeNode* s,int &i) { if(s!=NULL&&t!=NULL){ //如果左右子树都不为空 if(s->val!=t->val||s->val!=t->val) //判断左右子树的值是否不相等 { i=1; //不相等使i=1 r
2021-04-24 13:57:41
57
原创 c++层序遍历倒序输出
c++层序遍历倒序输出class Solution {public: vector<vector<int>> levelOrderBottom(TreeNode* root) { vector<vector<int>> res; int j=0; queue<TreeNode*> k; while(root==NULL) { retu
2021-04-24 11:50:01
344
原创 二叉树层次遍历
class Solution {public: vector<vector<int>> levelOrder(TreeNode* root) { queue<TreeNode*> r; //定义一个队列储存树节点 vector<vector<int>> res; //定义一个容器储存多个储存整数的容器 if(root==NULL) //如果根节点为空返回res容器 { ret
2021-04-23 19:36:17
84
原创 递归实现判断两棵树是否相同
class Solution {public: void check(TreeNode *p,TreeNode *q,int &i) { if(p!=NULL&&q!=NULL) { if(p->val!=q->val) { i=0; return; } check(p->le
2021-04-23 18:07:51
471
原创 中序遍历递归实现栈实现
class Solution {public:void inorder(TreeNode* root, vector& a) {if (!root) {return;}inorder(root->left, a);a.push_back(root->val);inorder(root->right, a);};vector<int> inorderTraversal(TreeNode* root) { vector<int> a;
2021-04-23 17:56:15
160
原创 涂方格
class Solution {public int paintingPlan(int n, int k) {int res = 0;//边界问题if(k == 0)return 1;if(k == n * n)return 1; //第一层循环表示涂 i 行 第二层循环表示涂 j 列 for(int i = 0;i <= n;i++){ for(int j = 0;j <= n;j++) //当你涂了 i 行 j 列 则有 i
2021-03-02 16:20:13
131
原创 奇数位于偶数前面
class Solution { public int[] exchange(int[] nums) { int left = 0; int right = nums.length - 1; while (left < right) { while (left < right && nums[left] % 2 != 0) { left++; }
2021-03-01 19:11:08
71
转载 石子游戏
class Solution {public: int stoneGameVI(vector<int>& aliceValues, vector<int>& bobValues) { vector<pair<int, int>> mp; //记录价值和以及下标 int n = aliceValues.size(); for(int i = 0; i < n; i++){ int dis = aliceValue
2021-02-27 20:47:56
84
转载 岛屿的周长
将这个“相邻关系”对应到 DFS 遍历中,就是:每当在 DFS 遍历中,从一个岛屿方格走向一个非岛屿方格,就将周长加 1。class Solution { public int islandPerimeter(int[][] grid) { for (int r = 0; r < grid.length; r++) { for (int c = 0; c < grid[0].length; c++) { if (grid[r][c] ==
2021-02-26 22:41:17
69
原创 设计哈希映射
class MyHashMap {public class Entry {public int key;public int value;public Entry(int k, int v) {key = k;value = v;}}private Entry[] data;private static final int SIZE = 13_333; // 10000 / 0.75private static final int DELETED = -1;public M
2021-02-26 22:03:21
55
原创 Java字符串压缩
class Solution { public String compressString(String S) { char[] s=S.toCharArray(); int n=S.length(); int k=1; StringBuilder str = new StringBuilder(n); for(int i=0;i<n;++i) { k=1; while(i<n-1&&s[i+
2021-02-25 20:17:00
104
转载 找树左下角的值
BFS用队列存储节点,先进先出从右往左遍历,也就是在往队列中添加数据时,先添加右子节点,再添加左子节点当队列为空时,循环结束,最后一个遍历到的节点就是最左边的节点返回最左边节点的值/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * Tre
2021-02-25 11:21:15
54
原创 转置矩阵
Javaclass Solution { public int[][] transpose(int[][] matrix) { int m=matrix.length; int n=matrix[0].length; int[][] k=new int[n][m]; for(int i=0;i<m;i++) { for(int j=0;j<n;j++) { k[j][i]=matrix[i]
2021-02-25 09:37:32
58
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人