用哈希表保存球队的名称和对应的得分,当某个球队的得分超过 n / 2 时,则该球队就一定是胜出的球队。
- #include <iostream>
- #include <cstring>
- #include <algorithm>
- #include <unordered_map>
-
- using namespace std;
-
- int main()
- {
- int n;
- cin >> n;
-
- unordered_map<string, int> score;
- for(int i = 0; i < n; i ++ ) {
- string s;
- cin >> s;
- score[s] ++;
-
- if(score[s] * 2 > n) {
- cout << s << endl;
- break;
- }
- }
-
- return 0;
- }
字符串res的前 n-3 项只由字符串s的前四个字符顺序构成,res最后3项由s的最后三个字符构成。
- #include <iostream>
- #include <cstring>
- #include <algorithm>
-
- using namespace std;
-
- int main()
- {
- int n;
- cin >> n;
-
- string s = "ROYGBIV";
- string res;
-
- for(int i = 0; i < n - 3; i ++ ) res += s[i % 4];
-
- for(int i = 0; i < 3; i ++ ) res += s[i + 4];
-
- cout << res << endl;
-
- return 0;
- }
https://leetcode.cn/problems/convert-the-temperature/按图索骥。
- class Solution {
- public:
- vector<double> convertTemperature(double celsius) {
- vector<double> res;
-
- res.push_back(celsius + 273.15);
- res.push_back(celsius * 1.80 + 32.00);
-
- return res;
- }
- };
https://leetcode.cn/problems/number-of-subarrays-with-lcm-equal-to-k/1、先找到每个等于k的数;
2、然后以该数为起点,想左向右遍历找到符合条件的数,组成一个子数组。
- class Solution {
- public:
- int gcd(int a, int b)
- {
- return b ? gcd(b, a % b) : a;
- }
- int subarrayLCM(vector<int>& nums, int k) {
- int res = 0;
- int n = nums.size();
- int l = 0, r = 0;
- for(int i = 0; i < n; i ++ ) {
- l = 0, r = 0;
- if(nums[i] == k) {
- res ++;
- //往右
- for(int j = i + 1; j < n; j ++ ) {
- if(nums[j] * k / gcd(nums[j], k) == k){
- res ++;
- r ++;
- }
- else break;
- }
- //往左
- for(int j = i - 1; j >= 0; j -- ) {
- if(nums[j] == k) break;
- if(nums[j] * k / gcd(nums[j], k) == k){
- res ++;
- l ++;
- }
- else break;
- }
- res += l * r;
- }
- }
-
-
- return res;
- }
- };
https://leetcode.cn/contest/weekly-contest-319/problems/minimum-number-of-operations-to-sort-a-binary-tree-by-level/1、把所有的元素都存入到数组中;
2、记录每个元素的起始位置,在记录每个元素排序后的例子;
3、建一个图,以排序后的元素指向该元素的起始位置建边;
4、统计连通块的数量,元素的个数减去连通块的数量就是答案。
- /**
- * Definition for a binary tree node.
- * struct TreeNode {
- * int val;
- * TreeNode *left;
- * TreeNode *right;
- * TreeNode() : val(0), left(nullptr), right(nullptr) {}
- * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
- * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
- * };
- */
- class Solution {
- public:
- vector<int> p;
-
- int find(int x) {
- if(x != p[x]) p[x] = find(p[x]);
- return p[x];
- }
-
- int minimumOperations(TreeNode* root) {
- queue<TreeNode*> q;
- q.push(root);
- //w:每层的序列;ls:每层的起点
- vector<int> w, ls;
- while(q.size()) {
- int sz = q.size();
- ls.push_back(w.size());
-
- for(int k = 0; k < sz; k ++ ) {
- auto t = q.front();
- q.pop();
- w.push_back(t->val);
- if(t->left) q.push(t->left);
- if(t->right) q.push(t->right);
- }
- }
-
- //保存每个点在原数组中的位置
- unordered_map<int, int> pos;
- for(int i = 0; i < w.size(); i ++ ) {
- pos[w[i]] = i;
- p.push_back(i);
- }
-
- ls.push_back(w.size());
- for(int i = 0; i + 1 < ls.size(); i ++ ) {
- sort(w.begin() + ls[i], w.begin() + ls[i + 1]);
- }
-
- //记录连通块的数量
- int cnt = w.size();
- for(int i = 0; i < w.size(); i ++) {
- int a = find(i), b = find(pos[w[i]]);
- if(a != b) {
- p[a] = b;
- cnt --;
- }
-
- }
- return w.size() - cnt;
- }
- };