A 特殊数组的特征值
暴力 模拟

1 class Solution { 2 public: 3 int specialArray(vector<int>& nums) { 4 int n = nums.size(); 5 for (int i = 1; i <= 100; i++) { 6 int m = 0; 7 for (int j = 0; j < n; j++) { 8 if (nums[j] >= i) m++; 9 } 10 if (m == i) return i; 11 } 12 return -1; 13 } 14 };
B 奇偶树
队列 模拟 层序遍历

1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 * }; 11 */ 12 class Solution { 13 public: 14 bool isEvenOddTree(TreeNode* root) { 15 TreeNode* p; 16 queue<TreeNode*> q; 17 q.push(root); 18 int d = 0; 19 while(!q.empty()) { 20 int n = q.size(); 21 int pre = (d % 2 == 0) ? -1 : 1e6 + 10; 22 for (int i = 0; i < n; i++) { 23 p = q.front(); q.pop(); 24 if ((p->val % 2) == (d % 2)) return false; 25 if ((d % 2) && pre <= p->val) return false; 26 else if(!(d % 2) && pre >= p->val) return false; 27 if (p->left != nullptr) q.push(p->left); 28 if (p->right != nullptr) q.push(p->right); 29 pre = p->val; 30 } 31 d++; 32 } 33 return true; 34 } 35 };
C 可见点的最大数目

1 class Solution { 2 public: 3 int visiblePoints(vector<vector<int>>& points, int ang, vector<int>& location) { 4 int ans = 0; 5 6 vector<double> angle; 7 8 for(const auto &point : points) { 9 int x = location[0] - point[0]; 10 int y = location[1] - point[1]; 11 if (x == 0 && y == 0) { 12 ans++; 13 } else { 14 angle.emplace_back(atan2(y, x) * 180 / M_PI); 15 } 16 } 17 18 sort(angle.begin(), angle.end()); 19 20 for(int i = 0, n = angle.size(); i < n; i++) { 21 angle.emplace_back(angle[i] + 360); 22 } 23 24 int anw = ans; 25 for(int i = 0, j = 0; i < angle.size(); i++) { 26 while(j < angle.size() && 27 (angle[j] - angle[i] < ang + 1e-9)) { 28 j++; 29 } 30 anw = max(j - i + ans, anw); 31 } 32 33 return anw; 34 } 35 };
格雷码解码过程(格雷码->二进制码): 从左边第二位起, 将每位与左边一位解码后的值异或, 作为该位解码后的值

1 class Solution { 2 public: 3 int minimumOneBitOperations(int n) { 4 int ans = 0; 5 while (n) { 6 ans ^= n; 7 n >>= 2; 8 } 9 return ans; 10 } 11 };