【leetcode】Weekly Contest 94

  题目不难,被第二题卡了半个多小时QAQ,另一个就是以后能用Hashmap和Hashset的绝不遍历。

1. Leaf-Similar Trees

  dfs、层次遍历把叶子节点遍历然后对比即可,只要是先遍历左节点后遍历右节点就行。

 1 class Solution {
 2     public boolean leafSimilar(TreeNode root1, TreeNode root2) {
 3         ArrayList<Integer> res1 = new ArrayList<>();
 4         ArrayList<Integer> res2 = new ArrayList<>();
 5         getLeaf(root1, res1);
 6         getLeaf(root2, res2);
 7         boolean equal = true;
 8         if(res1.size() == res2.size()){//叶子节点数量都不一样就不可能一样
 9             int i;
10             for(i = 0;i<res1.size();i++){
11                 if(res1.get(i) != res2.get(i)){
12                     equal = false;
13                     break;
14                 }
15             }
16         }
17         return equal;
18     }
19
20     void getLeaf(TreeNode root,ArrayList<Integer> res){//先序遍历获得所有叶子节点
21         if(root == null){
22             return ;
23         }
24         if(root.left == null && root.right ==null){
25             res.add(root.val);
26         }
27         getLeaf(root.left, res);
28         getLeaf(root.right, res);
29     }
30 }

874. Walking Robot Simulation

  被这题卡了好久,一开始想写的是判断起始点到末尾点中间是否有障碍物,前进的终点位置是最近的那个障碍物前一个位置,后来意识到前进的距离最多是9,直接一个点一个点判断就好了,这样可以直接用Hashset找是否有那个点。

 1     public static int robotSim(int[] commands, int[][] obstacles) {
 2         int[][] dir = {{0,1},{1,0},{0,-1},{-1,0}};
 3         int nowdir = 0;//当前方向
 4         int x = 0,y = 0;//记录当前位置
 5         int max = 0;
 6         int nx,ny;//下一个位置
 7         Set<Long> obstacleSet = new HashSet();
 8         for (int[] obstacle: obstacles) {
 9             long ox = (long) obstacle[0];
10             long oy = (long) obstacle[1];
11             obstacleSet.add((ox << 16) + oy);//前16位记录x的位置,后16位记录y的位置
12         }
13
14         for(int i = 0;i<commands.length;i++){
15             if(commands[i] == -1){
16                 nowdir = (nowdir+1)%4;
17             }else if (commands[i] == -2) {
18                 nowdir = (nowdir-1+4)%4;
19             }else {
20                 for (int k = 0; k < commands[i]; ++k) {
21                     nx = x + dir[nowdir][0];
22                     ny = y + dir[nowdir][1];
23                     long code = (((long) nx) << 16) + ((long) ny );
24                     if (!obstacleSet.contains(code)) {
25                         x = nx;
26                         y = ny;
27                         max = Math.max(max, x*x + y*y);
28                     }
29                 }
30             }
31         }
32         return max;
33     }

875. Koko Eating Bananas

  第三题不难,二分查找K的值(1-10^9),每次判断时间是否小于H即可,时间复杂度o(10^4*log10^9)。

 1 class Solution {
 2     public int minEatingSpeed(int[] piles, int H) {
 3         int up = 0;
 4         for (int i : piles) {//记录最大的数量,每小时吃的肯定不需要大于这个数量
 5             up = Math.max(up, i);
 6         }
 7         return find(piles,H,1,up);
 8     }
 9
10     static int find(int[] piles, int H,int min,int max){//二分搜索
11         if(min == max){
12             return min;
13         }else{
14             int mid = (min+max)/2;
15             // System.out.println(min +" "+ max+" "+mid);
16             if(eatAll(piles, H, mid)){//能吃完,就尝试更小的(当然mid也可能是所求值)
17                 return find(piles, H, min, mid);
18             }else {//吃不完,就每小时吃多点
19                 return find(piles, H, mid+1, max);
20             }
21         }
22     }
23
24     static boolean eatAll(int[] piles,int H,int K){
25         int need = 0;
26         for (int i : piles) {
27             need+=(i/K)+((i%K)==0?0:1);
28         }
29         return need<=H;
30     }
31 }

873. Length of Longest Fibonacci Subsequence

  这题思路对了,用二位数组dp[i][j]记录以A[i]为倒数第二个点,A[j]为最后一个点的最大斐波那契数列长度,o(n^2)遍历这个二维数组填充数据,再遍历一次取得结果即可。

 1 class Solution {
 2     public int lenLongestFibSubseq(int[] A) {
 3         int dp[][] = new int[A.length][A.length];
 4         HashMap<Integer, Integer> index = new HashMap<>();//记录数组值的下标
 5         for(int i = 0;i<A.length;i++){
 6             index.put(A[i], i);
 7         }
 8         for(int i = 2;i<A.length;i++){
 9             for(int j = 0;j<i;j++){
10                 Integer k = index.get(A[i] - A[j]);//查看是否存在需要的值
11                 if(k!=null && k<j){
12                     dp[i][j] = Math.max(dp[i][j], dp[j][k]+1);
13                 }
14             }
15         }
16         int res = 0;
17         for (int[] is : dp) {
18             for (int i : is) {
19                 res = Math.max(res, i);
20             }
21         }
22         return res==0?0:res+2;
23     }
24 }

  一开始没用Hashmap记录,因此需要第三个循环搜索是否存在需要的值,然后就tle惹,用Hashmap降了一个时间复杂度。

  本次题目都不是特别难,不过还是仰望那些20分钟AK的大佬(我看题目的时间可能都不止20min QAQ)。

原文地址:https://www.cnblogs.com/zzzdp/p/9351414.html

时间: 2024-07-29 13:54:49

【leetcode】Weekly Contest 94的相关文章

【leetcode】Weekly Contest 91

leetcode周赛,早上起来发现没网,用热点意识模糊的a了三个水题. 1.Lemonade Change 简单模拟题,收到十元用五元钱找回,收到20元时优先用一张10一张5,如果10不够用3张5,如果没有就返回flase(贪心). 1 public boolean lemonadeChange(int[] bills) { 2 int five = 0; 3 int ten = 0; 4 int twenty = 0; 5 for (int i = 0; i < bills.length; i

【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 递归和非递归,此提比较简单.广度优先遍历即可.关键之处就在于如何保持访问深度. 下面是4种代码: 1

【leetcode】557. Reverse Words in a String III

Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-in-a-string-iii/ 1)problem Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace

【leetcode】Generate Parentheses

题目: 给定整数n,返回n对匹配的小括号字符串数组. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()" 分析: 这种问题的模式是:1)问题的解有多个 ,2)每个解都是由多个有效的 "步骤" 组成的,3)变更以有解的某个或某些"步骤"

【LeetCode】Implement strStr()

Implement strStr() Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack. 标准KMP算法.可参考下文. http://blog.csdn.net/yaochunnian/article/details/7059486 核心思想在于求出模式串前缀与后缀中重复部分,将重复信息保存在n

【LeetCode】Add Two Numbers

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 ->

【LeetCode】Pascal&#39;s Triangle

Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 这题别想用通项公式做,n choose m里面的连乘必然溢出,老老实实逐层用定义做. class Solution { public: vector<vector<

【LeetCode】Copy List with Random Pointer

A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the list. 思路:第一遍正常复制链表,同时用哈希表保存链表中原始节点和新节点的对应关系,第二遍遍历链表的时候,再复制随机域. 这是一种典型的空间换时间的做法,n个节点,需要大小为O(n

【leetcode】Max Points on a Line (python)

给定一个点,除该点之外的其他所有点中,与该点的关系要么是共线,要么就是共点,也就是两点重合. 共线有三种情况:水平共线,垂直共线,倾斜的共线.合并下这三种情况就是斜率存在的共线和斜率不存在的共线. 那么我们的任务就是针对每个点,找出与其共线的这些情况中,共线最多的点的个数. 注意:最终的结果别忘了加上共点的个数. class Solution: def maxPoints(self, points ): if len( points ) <= 1: return len( points ) ma