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;
    }
};
相关推荐
夏幻灵8 小时前
每日一题:LeetCode 3:无重复字符的最长子串——滑动窗口思路详解
算法·leetcode·职场和发展
无忧.芙桃8 小时前
C++ 并查集详解:原理、实现与应用
c语言·c++·算法·leetcode
蛋蛋的就会顺顺的8 小时前
hot100——二叉树
java·数据结构·算法·leetcode·力扣
Nil20811 小时前
leetcode 55跳跃游戏
算法·leetcode·游戏
longlongzihan11 小时前
LeetCode 128. 最长连续序列 —— 从排序到哈希集合
数据结构·c++·算法·leetcode·排序算法·哈希表
圣保罗的大教堂13 小时前
leetcode 3302. 字典序最小的合法序列 中等
leetcode
Cccp.12314 小时前
【leetcode】(九)哈希函数与哈希表
数据结构·leetcode·哈希算法
xieliyu.14 小时前
LeetCode 1419 数青蛙:状态模拟求最少青蛙数量|Java 题解
java·算法·leetcode
Nil20814 小时前
leetcode 45跳跃游戏Ⅱ
算法·leetcode·游戏
longlongzihan15 小时前
LeetCode 283. 移动零:从辅助数组到双指针原地算法
c++·算法·leetcode