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;
    }
};
相关推荐
一匹电信狗8 小时前
【C++】异常详解(万字解读)
服务器·c++·算法·leetcode·小程序·stl·visual studio
墨染点香9 小时前
LeetCode 刷题【43. 字符串相乘】
算法·leetcode·职场和发展
Keying,,,,10 小时前
力扣hot100 | 矩阵 | 73. 矩阵置零、54. 螺旋矩阵、48. 旋转图像、240. 搜索二维矩阵 II
python·算法·leetcode·矩阵
_不会dp不改名_11 小时前
leetcode_42 接雨水
算法·leetcode·职场和发展
code小毛孩14 小时前
leetcode hot100数组:缺失的第一个正数
数据结构·算法·leetcode
快去睡觉~1 天前
力扣400:第N位数字
数据结构·算法·leetcode
gzzeason1 天前
LeetCode Hot100:递归穿透值传递问题
算法·leetcode·职场和发展
qq_513970441 天前
力扣 hot100 Day74
数据结构·算法·leetcode
墨染点香1 天前
LeetCode 刷题【42. 接雨水】
算法·leetcode·职场和发展
এ᭄画画的北北2 天前
力扣-347.前K个高频元素
算法·leetcode