1.27刷题记录

1.1207. 独一无二的出现次数 - 力扣(LeetCode)

cpp 复制代码
class Solution {
public:
    bool uniqueOccurrences(vector<int>& arr) {
        unordered_map<int,int> count_map;
        for(int ch:arr){
            count_map[ch]++;
        }
        unordered_set<int> count_set;
        for (auto [_, count] : count_map){
            count_set.insert(count);
        }
        return count_map.size()==count_set.size();
    }
};

学习:

  • 是否重复出现用set.size()==map.size()进行判断。
  • count_map的用法,count_mapch++;默认初始值为0.
  • for (auto _, count : count_map)中auto _,count:count_map中占位符的使用。

2.151. 反转字符串中的单词 - 力扣(LeetCode)

cpp 复制代码
class Solution {
public:
    std::string reverseWords(std::string s) {
        int n = s.size();
        string answer;
        int index = 0; // index用于过滤空格,存放对应的单词
        reverse(s.begin(), s.end()); // 先反转整个字符串

        for (int start = 0; start < n; start++) {
            if (s[start] != ' ') { // 如果不是空格
                int end = start;
                while (end < n && s[end] != ' ') { // 找到单词的结尾
                    end++;
                }
                // 反转当前单词
                reverse(s.begin() + start, s.begin() + end);
                // 将单词添加到结果中
                if (!answer.empty()) {
                    answer += ' '; // 如果结果不为空,添加空格
                }
                answer += s.substr(start, end - start);
                start = end - 1; // 更新start,跳过当前单词
            }
        }

        return answer;
    }
};
相关推荐
Navigator_Z3 小时前
LeetCode //C - 1089. Duplicate Zeros
c语言·算法·leetcode
语戚6 小时前
力扣 3161. 块放置查询:线段树解法(Java 实现)
java·算法·leetcode·面试·线段树·力扣·
8Qi88 小时前
LeetCode 23. 合并 K 个升序链表 —— 小顶堆(PriorityQueue)
数据结构·算法·leetcode·链表·
Lsk_Smion10 小时前
力扣实训 _ [200].岛屿数量
算法·leetcode·深度优先
Lsk_Smion12 小时前
力扣实训 _ [543].二叉树的直径 _ [23].合并K个升序列表
数据结构·算法·leetcode
凯瑟琳.奥古斯特12 小时前
力扣1235:加权区间调度最优解
java·python·算法·leetcode·职场和发展
memcpy013 小时前
LeetCode 2144. 打折购买糖果的最小开销【贪心】
算法·leetcode·职场和发展
散峰而望15 小时前
【算法练习】算法练习精选:陶陶摘苹果(基础+升级)、Music Notes、字串变换,你能AC几道?
数据结构·c++·算法·leetcode·贪心算法·github·动态规划
8Qi815 小时前
LeetCode 148. 排序链表 —— 解法一:自顶向下递归(分治 + 归并)
数据结构·算法·leetcode·链表·递归·分治·归并
菜菜的顾清寒16 小时前
力扣HOT100(50)动态规划-零钱兑换
算法·leetcode·动态规划