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;
    }
};
相关推荐
啊嘞嘞?2 小时前
力扣(LRU缓存)
算法·leetcode
啊嘞嘞?2 小时前
力扣(下一个排列)
算法·leetcode
Forever Nore3 小时前
LeetCode 16 最接近的三数之和 - 双指针逼近
算法·leetcode·职场和发展
smj2302_796826523 小时前
解决leetcode第4033题有效K个不同元素子数组I
数据结构·python·算法·leetcode
rannn_1113 小时前
【力扣hot100】回溯专题|全排列、子集、字母组合、组合总和、括号生成、单词搜索、分割回文串、N皇后
java·算法·leetcode·回溯
旖旎夜光4 小时前
LCR 173:在点名(二分查找) —— 题解
数据结构·c++·算法·leetcode·二分查找
重生之后端学习4 小时前
438. 找到字符串中所有字母异位词[中等]✅
开发语言·数据结构·算法·leetcode·职场和发展
evans在进步12 小时前
LeetCode 64:最小路径和——Java 原地动态规划详解
java·leetcode·动态规划
Tisfy12 小时前
LeetCode 1386.安排电影院座位:哈希表+位运算
算法·leetcode·散列表·题解·哈希表
Nil20813 小时前
leetcode 199二叉树的右视图
算法·leetcode·深度优先