AI写作智能体 自主规划任务,支持联网查询和网页读取,多模态高效创作各类分析报告、商业计划、营销方案、教学内容等。 广告
#### [剑指 Offer 56 - II. 数组中数字出现的次数 II](https://leetcode-cn.com/problems/shu-zu-zhong-shu-zi-chu-xian-de-ci-shu-ii-lcof/) 在一个数组 nums 中除一个数字只出现一次之外,其他数字都出现了三次。请找出那个只出现一次的数字。 示例 1: 输入:nums = [3,4,3,3] 输出:4 ```cpp class Solution { public: int singleNumber(vector<int>& nums) { int result = 0; for (int i = 0; i < 32; i++) { int cnt = 0; int byte = 1 << i; for (auto& x : nums) { if (x & byte) { cnt++; } } if (cnt % 3 == 1) { result |= byte; } } return result; } }; ``` #### [剑指 Offer 27. 二叉树的镜像](https://leetcode-cn.com/problems/er-cha-shu-de-jing-xiang-lcof/) ```cpp class Solution { public: TreeNode* mirrorTree(TreeNode* root) { if (root == NULL) return root; TreeNode* tmp = mirrorTree(root->left); root->left = mirrorTree(root->right); root->right = tmp; return root; } }; ``` #### [剑指 Offer 28. 对称的二叉树](https://leetcode-cn.com/problems/dui-cheng-de-er-cha-shu-lcof/) ```cpp class Solution { public: bool isSymmetric(TreeNode* root) { if (root == NULL) return true; return helper(root->left, root->right); } bool helper(TreeNode* left, TreeNode* right) { if (left == NULL && right == NULL) return true; if (left == NULL || right == NULL || left->val != right->val) return false; return helper(left->left, right->right) && helper(left->right, right->left); } }; ```