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_map[ch]++;默认初始值为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;
    }
};
相关推荐
帅小伙―苏13 分钟前
力扣42接雨水
前端·算法·leetcode
6Hzlia34 分钟前
【Hot 100 刷题计划】 LeetCode 287. 寻找重复数 | C++ 数组判环 (快慢指针终极解法)
c++·算法·leetcode
穿条秋裤到处跑6 小时前
每日一道leetcode(2026.04.19):下标对中的最大距离
算法·leetcode·职场和发展
木子墨5167 小时前
LeetCode 热题 100 精讲 | 计算几何篇:点积叉积 · 线段相交 · 凸包 · 多边形面积
c++·算法·leetcode·职场和发展·动态规划
py有趣7 小时前
力扣热门100题之最小路径和
算法·leetcode
im_AMBER7 小时前
Leetcode 159 无重复字符的最长子串 | 长度最小的子数组
javascript·数据结构·学习·算法·leetcode
郝学胜-神的一滴7 小时前
[力扣 105]二叉树前中后序遍历精讲:原理、实现与二叉树还原
数据结构·c++·算法·leetcode·职场和发展
sheeta19988 小时前
LeetCode 每日一题笔记 日期:2026.04.20 题目:2078.两栋颜色不同而距离最远的房子
笔记·算法·leetcode
承渊政道8 小时前
【递归、搜索与回溯算法】(floodfill算法:从不会做矩阵题,到真正掌握搜索扩散思想)
数据结构·c++·算法·leetcode·矩阵·dfs·bfs
剑挑星河月9 小时前
73.矩阵置零
数据结构·算法·leetcode·矩阵