- 博客(26)
- 收藏
- 关注
原创 并查集博客
python版本:https://blog.csdn.net/guoziqing506/article/details/78752557C++版本:https://blog.csdn.net/qq_41593380/article/details/81146850https://zhuanlan.zhihu.com/p/93647900例题:https://blog.csdn.net/guo15331092/article/details/78702686?tdsourcetag=s_pctim
2020-06-30 17:38:30 127
原创 leetcode:547. 朋友圈(dfs)
链接:https://leetcode-cn.com/problems/friend-circles/本来想用并查集写的,但感觉dfs还是简单点。创建一个集合标记已经访问过的人,然后挨个深度搜索访问没被访问过的人。注意count++的位置。C++代码:class Solution {public: int n; set<int> s; int findCircleNum(vector<vector<int>>& M) {
2020-06-30 17:31:39 182
原创 leetcode:1090. 受标签影响的最大值(贪心)
链接:https://leetcode-cn.com/problems/largest-values-from-labels/submissions/贪心问题,label和value都按value从大到小排序,然后取满足条件的尽可能大的即可。C++代码:class Solution {public: struct pair{ int value; int label; }; int largestValsFromLabels(vector<
2020-06-29 21:29:43 150
原创 leetcode : 1072. 按列翻转得到最大值等行数
链接:https://leetcode-cn.com/problems/flip-columns-for-maximum-number-of-equal-rows/经过若干次列翻转后,如果两行能同时满足行上所有值都相等,那么在翻转之前这两行要么相等要么互反(0-1)。因此统计最多的相等或互反的行数即可。可以借助把数组转为字符串的函数以及hashmap来求解。java代码:class Solution { public int maxEqualRowsAfterFlips(int[][] mat
2020-06-28 12:11:37 200
原创 leetcode:1053. 交换一次的先前排列
链接:https://leetcode-cn.com/problems/previous-permutation-with-one-swap/从后往前遍历,当前元素的后面有比它小的元素时,交换此元素与它后面比它小的最大元素。java代码:class Solution { public int[] prevPermOpt1(int[] A) { int n = A.length; int min = A[n-1];//当前最小值 int max =
2020-06-27 12:34:50 253
原创 leetcode:1029. 两地调度
链接:https://leetcode-cn.com/problems/two-city-scheduling/数学题,假设所有人先派去B点,然后在从中选出一半人派往A点,我们要使此时省下的钱最多,也就是选择cost(B)−cost(A)cost(B)-cost(A)cost(B)−cost(A)最大的一半人。C++代码:class Solution {public: int twoCitySchedCost(vector<vector<int>>& cost
2020-06-27 12:30:18 172
原创 leetcode:1040. 移动石子直到连续 II
链接:https://leetcode-cn.com/problems/moving-stones-until-consecutive-ii/注意到这道题每次移动石子后,边界都会变窄,对于最大的移动次数,我们要使每次移动边界变窄的量最小。首次移动时,边界变窄的量是由stones[1]−stones[0]stones[1]-stones[0]stones[1]−stones[0] 与stones[n−1]−stones[n−2]stones[n-1]-stones[n-2]stones[n−1]−stone
2020-06-27 12:28:07 197
原创 leetcode:1026. 节点与其祖先之间的最大差值(dfs)
链接:https://leetcode-cn.com/problems/maximum-difference-between-node-and-ancestor/遍历每条路径,寻找当前路径上的最大差值。dfs函数的参数为当前节点,路径上已遍历到的最大值与最小值。这种做法的关键在于祖先节点向子节点传递信息,在子节点处运算。当然用适当的数据结构记录左右子树分别的最大最小值然后在根节点运算也是一种解法。C++代码:/** * Definition for a binary tree node. * st
2020-06-24 12:22:48 181
原创 leetCode:1016. 子串能表示从 1 到 N 数字的二进制串
链接:https://leetcode-cn.com/problems/binary-string-with-substrings-representing-1-to-n/把整数转为字符串然后在S中查找即可。只需检查[N2+1,N][\frac{N}{2}+1,N][2N+1,N]的范围,因为2∗i2*i2∗i的二进制表示一定包含iii的二进制表示。java代码:class Solution { public boolean queryString(String S, int N) {
2020-06-20 10:32:39 208
原创 leetcode:1014. 最佳观光组合
链接:https://leetcode-cn.com/problems/best-sightseeing-pair/将公式分为A[i]+iA[i]+iA[i]+i与A[j]−jA[j]-jA[j]−j两部分,在统计景点jjj的答案时,由于A[j]−jA[j]-jA[j]−j是不变的,我们只需把它加上前面A[i]+iA[i]+iA[i]+i的最大值。因此当我们遍历数组时,记录下A[i]+iA[i]+iA[i]+i的最大值即可。java代码:class Solution { public int
2020-06-19 11:01:51 134
原创 leetcode:1007. 行相等的最少多米诺旋转
链接:https://leetcode-cn.com/problems/minimum-domino-rotations-for-equal-row/两次遍历,分别检查A[0]与B[0]能不能满足条件。class Solution { public int minDominoRotations(int[] A, int[] B) { int max1 = 0; boolean one = true; int sum1 = A.length;
2020-06-18 11:07:10 138
原创 leetcode: 1003. 检查替换后的词是否有效
链接:https://leetcode-cn.com/problems/check-if-word-is-valid-after-substitutions/把字符串中的abc一个一个地去掉,检查剩下的是否是空串即可。java代码:class Solution { public boolean isValid(String S) { while(S.contains("abc")) { S = S.replaceAll("abc","");
2020-06-17 10:13:40 193
原创 leetcode:998. 最大二叉树 II
链接:https://leetcode-cn.com/problems/maximum-binary-tree-ii/这道题理解了题目之后就不难做。三种情况:(1) root 为null,此时返回val单节点构成的树(2)root的值小于val,以val为根,root接在val的左子树上(必须是左子树)(3)root的值大于val,进入root的右子树递归(必须是右子树)java代码:/** * Definition for a binary tree node. * public clas
2020-06-15 10:33:22 133
原创 leetcode:994. 腐烂的橘子(bfs)
链接:https://leetcode-cn.com/problems/rotting-oranges/遍历原数组,将所有腐烂的橘子放入到队列中,最后再一起bfs。java代码:class Solution { public int orangesRotting(int[][] grid) { int m = grid.length; int n = grid[0].length; int maxtime = m*n;
2020-06-13 10:56:54 135
原创 leetcode:988. 从叶结点开始的最小字符串()
链接:https://leetcode-cn.com/problems/smallest-string-starting-from-leaf/submissions/dfs遍历树,查看每个可能的字符串,记录当前最小的字符串。/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeN
2020-06-12 11:20:57 185
原创 leetcode:983. 最低票价(动态规划)
链接:https://leetcode-cn.com/problems/minimum-cost-for-tickets/尝试买三种票,在当前天(及之前)获得最小票价。转移方程:dp[i]=min(cost[0]+dp[i−1],cost[1]+dp[i−7],cost[2]+dp[i−30])dp[i] = min(cost[0]+dp[i-1],cost[1]+dp[i-7],cost[2]+dp[i-30])dp[i]=min(cost[0]+dp[i−1],cost[1]+dp[i−7],cos
2020-06-11 11:13:01 182
原创 leetcode:980. 不同路径 III(dfs)
链接:https://leetcode-cn.com/problems/unique-paths-iii/dfs遍历即可,遍历过的方格标记为障碍物,回溯的时候再改回去。java代码:class Solution { int sum = 0; public int uniquePathsIII(int[][] grid) { int starti = 0; int startj = 0; for(int i = 0;i<grid.le
2020-06-10 11:08:26 274
原创 leetcode:979. 在二叉树中分配硬币
链接:https://leetcode-cn.com/problems/distribute-coins-in-binary-tree/需要创建一个函数,来计算一个子树中多了或少了几个金币,这样就可以算出一个节点需要分别向它的左右子节点传递(或被传递)多少金币。注意这道题里金币多了或少了,传递或被传递其实没有什么区别。java代码:/** * Definition for a binary tree node. * public class TreeNode { * int val;
2020-06-09 11:18:14 167
原创 leetcode: 967. 连续差相同的数字
链接:https://leetcode-cn.com/problems/numbers-with-same-consecutive-differences/递归即可,注意N=1与K=0时的处理。java代码:class Solution { List<Integer> ans = new ArrayList(); public int[] numsSameConsecDiff(int N, int K) { for(int i = 1;i<10;i++
2020-06-08 11:43:24 275
原创 leetcode: 950. 按递增顺序显示卡牌
链接:https://leetcode-cn.com/problems/reveal-cards-in-increasing-order/主要思想是模拟。想把数组排序,然后将数组下标塞进一个双端队列中,模拟他的抽牌放牌方法操作双端队列,队首的下标即为数组中现在遍历到的数在输出数组里应该在的下标。java代码:class Solution { public int[] deckRevealedIncreasing(int[] deck) { Arrays.sort(deck);
2020-06-07 12:13:17 138
原创 leetcode:916. 单词子集
链接:https://leetcode-cn.com/problems/word-subsets/将B中的单词整合为一个大单词记录A中单词的字幕出现频率,与大单词进行比较即可。java代码:class Solution { public List<String> wordSubsets(String[] A, String[] B) { List<String> ans = new ArrayList(); int []alphbetsi
2020-06-06 11:19:17 122
原创 leetcode: 894. 所有可能的满二叉树(记忆化递归)
链接:https://leetcode-cn.com/problems/all-possible-full-binary-trees/这道题的结构适合用递归来求解,注意偶数个节点个数一定不能构成满二叉树。递归地获得左右子树的满二叉树结构即可,为了避免重复计算,需要用一个map保存已经计算出的满二叉树结构。java代码:/** * Definition for a binary tree node. * public class TreeNode { * int val; * T
2020-06-05 11:13:06 162
原创 leetcode: 893. 特殊等价字符串组
链接:https://leetcode-cn.com/problems/groups-of-special-equivalent-strings/统计奇偶下标各字母的出现次数,或者分别对奇偶下标的子串进行排序。python代码:class Solution(object): def numSpecialEquivGroups(self, A): """ :type A: List[str] :rtype: int """
2020-06-04 11:17:52 124
原创 leetcode:890. 查找和替换模式
链接:https://leetcode-cn.com/problems/find-and-replace-pattern/创建数组(或哈希表),记录双射的信息即可。java代码:class Solution { public List<String> findAndReplacePattern(String[] words, String pattern) { List<String> res = new ArrayList(); for
2020-06-03 11:02:08 185
原创 leetcode: 889. 根据前序和后序遍历构造二叉树
链接:https://leetcode-cn.com/problems/construct-binary-tree-from-preorder-and-postorder-traversal/分别在前序遍历和后序遍历的数组里找到左右子树的分界点,递归求解即可。java代码:/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * Tr
2020-06-02 10:37:25 150
原创 leetcode: 881. 救生艇(贪心+双指针)
链接:https://leetcode-cn.com/problems/boats-to-save-people/先把数组排序,然后始终检查最大重量能否和最小重量上一艘船,若不行,则最大重量一人一艘。class Solution { public int numRescueBoats(int[] people, int limit) { Arrays.sort(people); int p1 = 0; int p2 = people.length-
2020-06-01 10:47:07 255
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人