第270场周赛小结

  • 我的Weekly Contest 270战况
  • 什么是LeetCode周赛?
  • show my code!
  • 复盘解决Hard题

我的Weekly Contest 270战况

参加周赛有7次了,1928/12931应该是目前最好一次排名了,上周的周赛和双周赛也是只做对3道题,我也是LeetCode周赛“三道题选手”啦。当然,离4道全AC的大佬们还有距离。

什么是LeetCode周赛?

刷LeetCode两年,我才发现leetcode上的算法题一直在更新,而且更新的题目的来源就是周赛(每周4题)和双周赛。

参加了几次,我的心得体会是:简单题不一定简单,中等/难题也不一定难,掌握会用常见套路,有了思路,再细心审题严谨分析。

show my code!

  • 前三道都是新瓶装旧酒,或许第一道medium应该为easy,easy应该为medium难度。
  • 第一题dfs全排列(47),很经典的题,不以0开头,偶数,三位数,加上去重,并不简单。
  • 第二题,删除链表的中间节点,链表归并排序(https://leetcode.com/problems/sort-list/solution/)时就实现过,快慢指针,要删除中间节点,需要记录前驱节点。
  • 第三题(2096),二叉树中两个节点的最短路径,依然可以从网上找到似曾相识的题,只不过题干有所改变,路径不再是节点的列表,而是每一步方向组成的字符串。
  • 解法:
  • 1、先求出两个节点的最近公共祖先(236)
  • 2、分别求出公共祖先到两个节点的路径
  • 3、根据两条路径拼接出方向字符串

2094. Finding 3-Digit Even Numbers

class Solution {private void dfs(int[] digits, boolean[] visited, int len, String cur, List<String> ans){if(len == 1){if(cur.charAt(0) == '0') return;}if(len == 3){if(((cur.charAt(2) - '0') & 1) == 0){ans.add(cur);}return;}for(int i = 0; i<digits.length; i++){if(visited[i]) continue;if(i > 0 && digits[i] == digits[i-1] && !visited[i-1]) continue;visited[i] = true;dfs(digits, visited, len+1, cur+digits[i], ans);visited[i] = false; }}public int[] findEvenNumbers(int[] digits) {Arrays.sort(digits);boolean[] visited = new boolean[digits.length];List<String> ans = new ArrayList<>();dfs(digits, visited, 0, "", ans);int[] nums = new int[ans.size()];for(int i=0; i<ans.size(); i++){nums[i] = Integer.parseInt(ans.get(i));}return nums;}
}

2095. Delete the Middle Node of a Linked List

/*** Definition for singly-linked list.* public class ListNode {*     int val;*     ListNode next;*     ListNode() {}*     ListNode(int val) { this.val = val; }*     ListNode(int val, ListNode next) { this.val = val; this.next = next; }* }*/
class Solution {public ListNode deleteMiddle(ListNode head) {if(head == null || head.next == null) return null;ListNode p = head;ListNode midPrev = null;while (p != null && p.next != null) {midPrev = (midPrev == null) ? p : midPrev.next;p = p.next.next;}midPrev.next = midPrev.next.next;return head;}
}

2096. Step-By-Step Directions From a Binary Tree Node to Another
用例1:
用例2:

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode() {}*     TreeNode(int val) { this.val = val; }*     TreeNode(int val, TreeNode left, TreeNode right) {*         this.val = val;*         this.left = left;*         this.right = right;*     }* }*/
class Solution {public String getDirections(TreeNode root, int startValue, int destValue) {ArrayList<TreeNode> pathList1=new ArrayList<>();ArrayList<TreeNode> pathList2=new ArrayList<>();//求出两个节点的最近公共祖先TreeNode ancestor=lowestCommonAncestor(root, startValue, destValue);//分别求出公共祖先到两个节点的路经getPath(ancestor,startValue,pathList1);getPath(ancestor,destValue,pathList2);StringBuilder sb = new StringBuilder();for(int i=0; i<pathList1.size()-1; i++){sb.append("U");}for(int i=0; i<pathList2.size()-1; i++){TreeNode pnt = pathList2.get(i);TreeNode cid = pathList2.get(i+1);if(pnt.left == cid) sb.append("L");else sb.append("R");}return sb.toString();}public TreeNode lowestCommonAncestor (TreeNode root, int node1, int node2){if (root==null || root.val==node1 || root.val==node2) {return root;}TreeNode left = lowestCommonAncestor(root.left,node1,node2);TreeNode right = lowestCommonAncestor(root.right, node1, node2);if(left != null && right != null) { return root;}return left==null?right:left; }/***  获取祖先节点到目标节点的路经(包含祖先节点和目标节点)*/public boolean getPath(TreeNode root,int target,ArrayList<TreeNode> pathList){pathList.add(root);          if (root.val == target) {return true; }         boolean hasFound=false;if (root.left!=null)hasFound=getPath(root.left,target,pathList);if (!hasFound && root.right!=null)hasFound=getPath(root.right,target,pathList);if (!hasFound)pathList.remove(pathList.size()-1);return hasFound; }
}

复盘解决Hard题

2097. Valid Arrangement of Pairs
比赛结束后,官网有解题提示,正好学习一波。

欧拉回路:从起点出发,每条边走一遍,遍历所有结点和边后回到起点,这个路径称为欧拉回路。
找欧拉回路的Hierholzer算法思路如下:

The algorithm assumes that the given graph has a Eulerian Circuit.

  • Choose any starting vertex v, and follow a trail of edges from that vertex until returning to v. It is not possible to get stuck at any vertex other than v, because indegree and outdegree of every vertex must be same, when the trail enters another vertex w there must be an unused edge leaving w.
    The tour formed in this way is a closed tour, but may not cover all the vertices and edges of the initial graph.
  • 上面的一段,大意是从结点v dfs直到回到v,但一次dfs可能不会遍历完所有结点和边。
  • As long as there exists a vertex u that belongs to the current tour, but that has adjacent edges not part of the tour, start another trail from u, following unused edges until returning to u, and join the tour formed in this way to the previous tour.
  • 回溯,上一步的路径上存在结点u还有未访问的边, 从结点u出发,访问所有未访问的边能回到u。和之前的路径汇合形成欧拉回路。
  • 算法的具体实现可参照题解的dfs方法,其中ans是逆序的欧拉回路路径。
private void dfs(int start)
{List<Integer> edges = graph.get(start);if(edges == null) return;while(edges.size() > 0){int next = edges.get(edges.size()-1);edges.remove(edges.size()-1);dfs(next);//when next has not adjoin edge, //add [start, next] to ansans.add(new int[]{start, next});}
}

那么,这一题与欧拉回路稍许不同的是,遍历完所有的边不一定回到起点。
那么,dfs的起点就不能随意指定,而是要找(出度>入度)的节点作为起点,如果不存在,则按照欧拉回路的解法随意指定起点,如pairs[0][0]。

class Solution {Map<Integer, List<Integer>> graph;Map<Integer, Integer> degree;List<int[]> ans;public int[][] validArrangement(int[][] pairs) {graph = new HashMap<>();ans = new ArrayList<>();degree = new HashMap<>();//build graphbuildGraph(pairs);//select a startint start = -1;for(Integer i : degree.keySet()){if(degree.get(i) > 0){start = i;break;}}if(start == -1) start = pairs[0][0];//dfs Hierholzerdfs(start);int size = ans.size();int[][] range = new int[size][2];for(int i=0; i<size; i++){range[i][0] = ans.get(size-1-i)[0];range[i][1] = ans.get(size-1-i)[1];}return range;}private void buildGraph(int[][] pairs){for(int[] pair : pairs){graph.putIfAbsent(pair[0], new ArrayList<>());List<Integer> edges = graph.get(pair[0]);edges.add(pair[1]);int cnt = degree.getOrDefault(pair[0], 0);degree.put(pair[0], cnt+1);cnt = degree.getOrDefault(pair[1], 0);degree.put(pair[1], cnt-1);}}private void dfs(int start){List<Integer> edges = graph.get(start);if(edges == null) return;while(edges.size() > 0){int next = edges.get(edges.size()-1);edges.remove(edges.size()-1);dfs(next);//when next has not adjoin edge, //add [start, next] to ansans.add(new int[]{start, next});}}
}

我也是LeetCode周赛“三道题选手”啦 第270场周赛相关推荐

  1. 20211205:力扣第270场周赛(上)

    力扣第270场周赛(上) 题目 思路与算法 代码实现 写在最后 题目 找出 3 位偶数 删除链表的中间节点 思路与算法 找出 3 位偶数:遍历所有数字得到3位数字,前导不为0即表示大于等于100,先用 ...

  2. Leetcode之第294场周赛小记

    小记 本篇博客记录小黑第三次参加leetcode周赛(294场次)的成绩,以及对题目的总结,以便鞭策自己不断前进 . 这次周赛是我第三次参加,前两题比较简单,做起来也是非常顺利,只用了15分钟就完成解 ...

  3. [LeetCode周赛复盘] 第 324 场周赛20221218

    [LeetCode周赛复盘] 第 324 场周赛20221218 一.本周周赛总结 二. [Easy] 6265. 统计相似字符串对的数目 1. 题目描述 2. 思路分析 3. 代码实现 三.[Med ...

  4. Leetcode之第297场周赛小记

    小记 本篇博客记录小黑第六次参加leetcode周赛(297场次)的成绩,以及对题目的总结,以便鞭策自己不断前进 . 下面主要是对题目进行分析总结,也是为之后面试做准备. 题目一:2303. 计算应缴 ...

  5. LeetCode第176场周赛(Weekly Contest 176)解题报告

    又是一周掉分之旅,我发现,LeetCode周赛的数据好水,所以有的时候,实在没思路,先暴力解决试试(即使分析出时间复杂度会超时),比如第二题和第三题都可以暴力通过,GG思密达. 这周主要使用了数据结构 ...

  6. Leetcode第286场周赛

    绪论 上周因为有事没有参加周赛,这周没有错过.这次周赛拿到了人生第一个AK,参加大大小小的比赛这么多次,从来没有AK过,泪目了. 感觉这次比赛的思维难度对我来讲稍高一些,前三道题就花了一个小时,而以往 ...

  7. leetcode 刷500道题,笔试/面试稳吗?谈谈算法的学习

    来源公众号:苦逼的码农 作者:帅地 想要学习算法.应付笔试或者应付面试手撕算法题,相信大部分人都会去刷 Leetcode,有读者问?如果我在 leetcode 坚持刷它个 500 道题,以后笔试/面试 ...

  8. [算法]LeetCode第194场周赛202006021

    第194场周赛 20200621 1486. 数组异或操作 题目描述1 给你两个整数,n 和 start . 数组 nums 定义为:nums[i] = start + 2*i(下标从 0 开始)且 ...

  9. LeetCode第 310 场周赛

    文章目录 前言 第86场双周赛情况 题目复盘+题解 题1:6176. 出现最频繁的偶数元素[easy] 题2:6176. 出现最频繁的偶数元素[medium] 题3:6178. 将区间分为最少组数[m ...

最新文章

  1. mysql jdbc dbcp_连接数据库 JDBC、DBCP、JNDI
  2. sqlite3admin触发器创建
  3. Photon Server伺服务器在LoadBalancing的基础上扩展登陆服务
  4. 「 深入浅出 」集合List
  5. Invocation of init method failed; nested exception is org.hibernate.InvalidMappingException: Could n
  6. CLR Via CSharp读书笔记(14):字符、字符串和文本处理
  7. 10.14.2 快捷键,环境变量等
  8. ES6模块的import和export用法总结
  9. github单独下载某个文件或文件夹
  10. 使用 IntraWeb (39) - THttpRequest、THttpReply
  11. 极简风海报作品合集|过目不忘的海报大片,越看越过瘾
  12. JAVA导出404_从ActiveReports 8导出Excel时出错404
  13. 进入顶尖行业的人,都是把职业当乐趣的
  14. 中标麒麟linux系统安装打印机_国产操作系统中标麒麟系统安装教程
  15. WPS Office 2016 专业增强精简版 附终身授权正版序列号
  16. C语言考试题目(一)
  17. (转)远程计算机或设备将不接受连接,谷歌浏览器无法上网
  18. TCP/IP详解--拥塞控制机制 RTT时间计算相关的算法
  19. 芯片在计算机中作用是什么,逻辑芯片的作用是什么
  20. IE10的网站兼容性问题

热门文章

  1. 关于面试,我有大技巧
  2. 火山PC_HPSocket_tcp通信教程
  3. 十六,洗礼 《西敏小要理问答》
  4. java面试题及答案2022,java2022最新面试题及答案
  5. 获取枚举类型的名字字符串的方法
  6. 欢迎大家my blog in the CSDN RDF instruction for JAVA
  7. python目录及文件操作_python路径及文件操作,10.22,10.27
  8. 【论文解读 IJCAI 2019 | ActiveHNE】Active Heterogeneous Network Embedding
  9. 解决 iOS 15 上 APP 莫名其妙地退出登录
  10. 深圳华瑞三和集团有限公司携手泛微华南大区