Leet Code 第 132 场周赛

5024. 除数博弈

题目链接

题解:分析Alice和Bob的操作我们可以知道:

N 1 2 3 4
先手胜负

同时我们可以发现,当N为奇数时先手要么取1要么取一个N的因数x, N-x为偶数

当N为偶数时,此时的先手则可以取1,N-1为奇数

所以先手拿到奇数必输,拿到偶数必赢。

例如,N=5,则先手必输,因为其只能取1,则N1 = N-1 = 4,为当前先手必胜态(即后手Bob必胜)

N = 6,则先手可以拿1,则N1 = N-1 = 5, 则此时为当前先手必败态(即后手Bob必败)

...

N = 9,则先手只能拿1或者3,但是N-1 = 8, N-3 = 6皆为偶数,为当前先手必胜态(即后手Bob必胜),所以先手Alice必败。

 

代码:

class Solution {
    public boolean divisorGame(int N) {
        return N%2 == 0;
    }
}

 

5030. 节点与其祖先之间的最大差值

题目链接

题解:直接dfs即可。

代码:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    
    public int dfs(TreeNode root, int x,int y)
    {
        if(root == null) return 0;
        x = Math.max(x,root.val);
        y = Math.min(y,root.val);
        return Math.max(x-y,Math.max(dfs(root.left,x,y),dfs(root.right,x,y)));
    }
    public int maxAncestorDiff(TreeNode root) {
        return dfs(root,root.val,root.val);
    }
}

 

5025. 最长等差数列

题目链接

题解:可以视作特殊的区间DP,DP方程为 dp[i][j] = dp[k][i] + 1, k为小于且距离i最近的前一项的下标, k = b[2*A[i]-A[j]] , b[A[i]] = i;

代码:

class Solution {
    public int b[] = new int[10010];
    public int dp[][] = new int[2010][2010];
    public int longestArithSeqLength(int[] A) {
        int n = A.length;
        int ans = 1;
        for(int i = 0; i < n; i++)
            for(int j = i+1; j < n; j++)
                dp[i][j] = 2;
        Arrays.fill(b,-1);
        
        for(int i = 0; i < n; i++)
        {
            for(int j = i+1; j < n; j++)
            {
                int k = 2*A[i]-A[j];
                if(k >= 0 && k <= 10000 && b[k] != -1)
                dp[i][j] = dp[b[k]][i] + 1;
            }
            b[A[i]] = i;
         }
        for(int i = 0; i < n; i++)
            for(int j = i+1; j < n; j++)
                ans = Math.max(ans,dp[i][j]);
        return ans;
    }
}

5031. 从先序遍历还原二叉树

题目链接

题解:线性扫描字符串S,并结合深度进行递归调用,有点考验代码功底。

代码:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */

public class Inte{
    public int x;
    Inte(int v) {x = v;}
}

class Solution {
        
    public TreeNode dfs(TreeNode root, String S,  Inte x, Inte y)
    {
        //System.out.println("*"+ x.x);
        int ty = y.x;
        y.x = 0;
        while(x.x < S.length() && S.charAt(x.x) == '-')  {x.x ++;y.x ++;}
        int res = 0;
        while(x.x < S.length() && S.charAt(x.x) != '-')
              {
                  res *= 10;
                  res += (S.charAt(x.x) - '0');
                  x.x++;
              }
        TreeNode node = new TreeNode(res);
        if(ty+1 == y.x)
        {
            //System.out.println("*"+ res);
            if(root.left == null) 
            {
                //System.out.println("*"+ res);
                root.left = node;
                TreeNode temp = dfs(node,S,x,y);
                if(ty+1 == y.x)
                {
                    root.right = temp;
                    return dfs(temp,S,x,y);
                }
                else return temp;
            }
            else {
                root.right = node;
                return dfs(node,S,x,y);
              }   
        }
        else return new TreeNode(res);
    }
    
    public TreeNode recoverFromPreorder(String S) {
        int x = 0,y = 0,res = 0;
         while(x < S.length() && S.charAt(x) != '-')
              {
                  res *= 10;
                  res += (S.charAt(x) - '0');
                  x++;
              }
        TreeNode root = new TreeNode(res);
        dfs(root,S,new Inte(x),new Inte(y));
        return root;
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章