LeetCode - Weekly Contest - 132

1025. Divisor Game - Easy
1026. Maximum Difference Between Node and Ancestor - Medium
1027. Longest Arithmetic Sequence - Medium
1028. Recover a Tree From Preorder Traversal - Hard

1025. Divisor Game

  Alice and Bob take turns playing a game, with Alice starting first.
  Initially, there is a number N on the chalkboard. On each player’s turn, that player makes a move consisting of:
  1、Choosing any x with 0 < x < N and N % x == 0.
  2、Replacing the number N on the chalkboard with N - x.
  Also, if a player cannot make a move, they lose the game.
  Return True if and only if Alice wins the game, assuming both players play optimally.

Example 1:
Input: 2
Output: true
Explanation: Alice chooses 1, and Bob has no more moves.

Example 2:
Input: 3
Output: false
Explanation: Alice chooses 1, Bob chooses 1, and Alice has no more moves.

Solution:
  从 Alice 开始,如果 N 是 2,Alice 选 x 为 1,N 变成 1,到了 Bob 没有可以选的了,所以 Alice 赢了,N 是 3 的话,Alice 就输了,N 是 4 的话,Alice可以选 x 为 2,但是这样就输了,所以 Alice 选 x 为1,让 N 变成3 到 B,以此类推,N 的初始值是偶数的话,Alice 肯定赢,因为她可以选 x 为 1,把 N - 1 给 Bob,因为 N - 1 是奇数,所以 B 选的 x 一定也是奇数,A 的值就一定还是偶数,那 A 还选 x 为 1 即可,直到最后,一定是 A 是 2,A 选 x 为 1,B 没得选,A 赢。 同理,初始值为奇数的话,A 一定输。

bool divisorGame(int N) {
	return N % 2 == 0;
}

1026. Maximum Difference Between Node and Ancestor

  Given the root of a binary tree, find the maximum value V for which there exists different nodes A and B where V = |A.val - B.val| and A is an ancestor of B.
  (A node A is an ancestor of B if either: any child of A is equal to B, or any child of A is an ancestor of B.)

Example:
Input: [ 8, 3, 10, 1, 6, null, 14, null, null, 4, 7, 13 ]
Output: 7
Explanation:
We have various ancestor-node differences, some of which are given below :
  |8 - 3| = 5
  |3 - 7| = 4
  |8 - 1| = 7
  |10 - 13| = 3
Among all possible differences, the maximum value of 7 is obtained by |8 - 1| = 7.

Solution:
  其实就是 dfs 就行,每次记录这一层的最大最小值就行。

    void dfs(TreeNode* nod, int cur_max, int cur_min, int& max_diff)
    {
        if(!nod) return;
        if(nod->val > cur_max)
        {
            max_diff = max(max_diff, nod->val - cur_min);
            cur_max = nod->val;
        }
        else if(nod->val < cur_min)
        {
            max_diff = max(max_diff, cur_max - nod->val);
            cur_min = nod->val;
        }
        dfs(nod->left,  cur_max, cur_min, max_diff);
        dfs(nod->right, cur_max, cur_min, max_diff);
    }
    
    int maxAncestorDiff(TreeNode* root) {
        int max_diff = 0;
        dfs(root, root->val, root->val, max_diff);
        return max_diff;
    }

1027. Longest Arithmetic Sequence

  Given an array A of integers, return the length of the longest arithmetic subsequence in A.
  Recall that a subsequence of A is a list A[i_1], A[i_2], …, A[i_k] with 0 <= i_1 < i_2 < … < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).

Example 1:
Input: [3,6,9,12]
Output: 4
Explanation:
The whole array is an arithmetic sequence with steps of length = 3.

Example 2:
Input: [9,4,7,2,10]
Output: 3
Explanation:
The longest arithmetic subsequence is [4,7,10].

Example 3:
Input: [20,1,15,3,10,5,8]
Output: 4
Explanation:
The longest arithmetic subsequence is [20,15,10,5].

Solution:
  正常的dp题,用一个 vector<unordered_map<int, int>> a; a[i][j] 表示从第 i 位往前,有 j 这么大的差的等差数列有多长,因为 1 2,这样的等差数列有 1 个空,所以等差数列的长度还要加1。

int longestArithSeqLength(vector<int>& A) {
	int res = 0;
	const int len = A.size();
	if (len <= 1) return len;
	vector<unordered_map<int, int>> vec(len);
	for(int i = 1; i < len; ++i)
		for(int j = 0; j < i; ++j)
		{
			const int diff = A[i] - A[j];
			vec[i][diff] = vec[j][diff] + 1;
			res = max(res, vec[i][diff]);
		}
	return res + 1;
}

1028. Recover a Tree From Preorder Traversal

  We run a preorder depth first search on the root of a binary tree.
  At each node in this traversal, we output D dashes (where D is the depth of this node), then we output the value of this node. (If the depth of a node is D, the depth of its immediate child is D+1. The depth of the root node is 0.)
  If a node has only one child, that child is guaranteed to be the left child.
  Given the output S of this traversal, recover the tree and return its root.

Solution:
  其实就是用一个string 重建二叉树,0 个 dash 后边的是根节点,1个的是第一层,两个的是第二层。我的想法就是直接递归,每次就建出根节点,在对左右节点递归,这之间只要写一个函数对string进行操作,每次把连续的dash删掉一个就行。

string remove_dashes(const string& s)
{
	// 这个函数删除每个连续'-'中的一个
	string res;
	bool to_delete = true;
	for(auto c : s)	{
		if(c == '-' && to_delete){
			to_delete = false;
			continue;
		}
		res += c;
		if(c != '-')
			to_delete = true;
	}
	return res;
}

TreeNode* recoverFromPreorder(string S) {
	// 找到左子树、右子树的根节点,也就是单独的两个'-'的位置
	int left = -1, right = -1;
	for (int i = 1; i < S.length() - 1; ++i)
		if (S[i] == '-' && S[i - 1] != '-' && S[i + 1] != '-')
		{
			if (left == -1) left = i;
			else            right = i;
		}
	// 计算根节点的值
	int root_val = 0;
	for(auto s : S)
	{
		if (s == '-') break;
		root_val = root_val * 10 + s - '0';
	}
	auto root = new TreeNode(root_val);
	if (left != -1)	 root->left  = recoverFromPreorder(remove_dashes(S.substr(left + 1, right - left - 1)));
	if (right != -1) root->right = recoverFromPreorder(remove_dashes(S.substr(right + 1)));
	return root;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
LeetCode-Editor是一种在线编码工具,它提供了一个用户友好的界面编写和运行代码。在使用LeetCode-Editor时,有时候会出现乱码的问题。 乱码的原因可能是由于编码格式不兼容或者编码错误导致的。在这种情况下,我们可以尝试以下几种解决方法: 1. 检查文件编码格式:首先,我们可以检查所编辑的文件的编码格式。通常来说,常用的编码格式有UTF-8和ASCII等。我们可以将编码格式更改为正确的格式。在LeetCode-Editor中,可以通过界面设置或编辑器设置来更改编码格式。 2. 使用正确的字符集:如果乱码是由于使用了不同的字符集导致的,我们可以尝试更改使用正确的字符集。常见的字符集如Unicode或者UTF-8等。在LeetCode-Editor中,可以在编辑器中选择正确的字符集。 3. 使用合适的编辑器:有时候,乱码问题可能与LeetCode-Editor自身相关。我们可以尝试使用其他编码工具,如Text Editor、Sublime Text或者IDE,看是否能够解决乱码问题。 4. 查找特殊字符:如果乱码问题只出现在某些特殊字符上,我们可以尝试找到并替换这些字符。通过仔细检查代码,我们可以找到导致乱码的特定字符,并进行修正或替换。 总之,解决LeetCode-Editor乱码问题的方法有很多。根据具体情况,我们可以尝试更改文件编码格式、使用正确的字符集、更换编辑器或者查找并替换特殊字符等方法来解决这个问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值