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;
    }
};
相关推荐
zander25833 分钟前
35. 搜索插入位置:从边界语义理解二分查找
数据结构·算法·leetcode
yyds_yyd_100861 小时前
3731. 找出缺失的元素(2026.08.04)
c++·leetcode
lueluelue4710 小时前
LeetCode:链表
算法·leetcode·链表
橘子汽水16813 小时前
Leetcode 23,543合并K个升序链表,二叉树的直径
算法·leetcode·链表
Re.不晚16 小时前
挑战做100道力扣算法- DAY1
算法·leetcode·职场和发展
青山木17 小时前
Hot 100 --- 搜索插入位置
java·数据结构·算法·leetcode
星轨初途20 小时前
LeetCode 热题 100——day2 字母异位词分组
c++·算法·leetcode
雪碧聊技术1 天前
力扣 72. 编辑距离——动态规划经典例题
算法·leetcode·动态规划
木井巳1 天前
【DFS解决floodfill算法】岛屿的最大面积
java·算法·leetcode·深度优先
Tisfy1 天前
LeetCode 1406.石子游戏 III:递归(DFS+记忆化) / 递推(DP+原地滚动)
leetcode·游戏·深度优先·dfs·题解·博弈