当前位置: 首页 > news >正文

力扣学习笔记——49. 字母异位词分组

49. 字母异位词分组

https://leetcode.cn/problems/group-anagrams/?envType=study-plan-v2&envId=top-100-liked
给你一个字符串数组,请你将 字母异位词 组合在一起。可以按任意顺序返回结果列表。

字母异位词 是由重新排列源单词的所有字母得到的一个新单词。

示例 1:

输入: strs = [“eat”, “tea”, “tan”, “ate”, “nat”, “bat”]
输出: [[“bat”],[“nat”,“tan”],[“ate”,“eat”,“tea”]]
示例 2:

输入: strs = [“”]
输出: [[“”]]
示例 3:

输入: strs = [“a”]
输出: [[“a”]]

这个首先是排序,排序后用哈希表将结果和排序的字符串作为键对应起来

知识点

C++ 排序的库函数使用

在 C++ 中,你可以使用 头文件中的库函数来进行排序。以下是一些常用的排序函数:

std::sort():对容器或指定范围内的元素进行排序,默认按升序排序。注意:std::sort(),不光能对数字进行排序,还可以对字母进行排序。

#include <algorithm>
#include <vector>int main() {std::vector<int> nums = {5, 2, 8, 1, 9};std::sort(nums.begin(), nums.end());// 输出排序后的结果for (const auto& num : nums) {std::cout << num << " ";}return 0;
}

输出结果:1 2 5 8 9

std::stable_sort():与 std::sort() 类似,但保持相等元素的相对顺序不变。

#include <algorithm>
#include <vector>int main() {std::vector<int> nums = {5, 2, 8, 1, 9};std::stable_sort(nums.begin(), nums.end());// 输出排序后的结果for (const auto& num : nums) {std::cout << num << " ";}return 0;
}

输出结果:1 2 5 8 9

自定义排序:你还可以使用自定义的比较函数来进行排序,例如按照特定的条件进行排序。

#include <algorithm>
#include <vector>bool customCompare(int a, int b) {// 自定义排序规则:按照奇偶性进行排序if (a % 2 == 0 && b % 2 != 0) {return false;  // a 在 b 前面} else if (a % 2 != 0 && b % 2 == 0) {return true;   // a 在 b 后面} else {return a < b;  // 其他情况按照升序排序}
}int main() {std::vector<int> nums = {5, 2, 8, 1, 9};std::sort(nums.begin(), nums.end(), customCompare);// 输出排序后的结果for (const auto& num : nums) {std::cout << num << " ";}return 0;
}

输出结果:1 5 9 2 8

这些是一些常用的排序函数和用法。你可以根据实际需求选择适合的排序函数,并根据需要编写自定义的比较函数

如何用for循环如何拿到std::unordered_map的键和值

如果你想使用 for 循环来获取 std::unordered_map 的键和值,你可以使用迭代器来遍历容器。每个迭代器指向一个键值对,你可以通过解引用迭代器来获取键和值。

以下是一个示例代码,展示了如何使用 for 循环遍历 std::unordered_map 并获取键和值:

#include <iostream>
#include <unordered_map>int main() {std::unordered_map<int, std::string> myMap = {{1, "apple"}, {2, "banana"}, {3, "orange"}};// 遍历 unordered_mapfor (auto it = myMap.begin(); it != myMap.end(); ++it) {int key = it->first;std::string value = it->second;std::cout << "Key: " << key << ", Value: " << value << std::endl;}return 0;
}

在上面的示例中,我们使用 auto 关键字来推导出迭代器的类型。然后,使用 begin() 函数获取指向第一个键值对的迭代器,使用 end() 函数获取指向最后一个键值对之后位置的迭代器。在循环中,通过解引用迭代器来获取当前键值对的键和值。

输出结果将是:

Key: 1, Value: apple
Key: 2, Value: banana
Key: 3, Value: orange

你可以根据实际需要修改键和值的类型,并在循环中执行适当的操作

http://www.lryc.cn/news/205049.html

相关文章:

  • 五、Qt中的常用类
  • CentOS 7.9.2009 数据盘挂载
  • cv::solvePnP使用方法及注意点详解(OpenCV/C++)
  • DevOps持续集成-Jenkins(4)
  • 【数据仓库-零】数据仓库知识体系 ing
  • css3 3D 转换 技巧详细解析与代码实例
  • [Unity]给场景中的3D字体TextMesh增加描边方案一
  • TDengine(taos)数据库导出历史数据
  • 算法进修Day-37
  • 服务器之日常整活
  • 交互式 Web 应用 0 基础入门
  • JSONP的安全性较差,那么在跨域情况下,有没有其他更安全的替代方案呢?
  • Slax Linux 获得增强的会话管理和启动参数选项
  • C/C++新冠疫情死亡率 2020年9月电子学会青少年软件编程(C/C++)等级考试一级真题答案解析
  • Adobe Photoshop 基本操作
  • SpringMVC原理及核心组件
  • 【rk3568-linux】 rk3568x_linux-- 编译说明
  • 模拟计算器编程教程,中文编程开发语言工具编程实例
  • Spring Security漏洞防护—HTTP 安全响应头
  • Plooks大型视频在线一起看网站源码
  • 图像处理中底层、高层特征、上下文信息理解
  • 负载均衡的算法(静态算法与动态算法)
  • mac安装jdk
  • WIN11+OPENCV4.8 编译及下载失败处理方法
  • 万宾科技智能井盖传感器怎么使用?
  • Server Name Indication(SNI),HTTP/TLS握手过程解析
  • react项目实现文件预览,比如PDF、txt、word、Excel、ppt等常见文件(腾讯云cos)
  • ES SearchAPI----Query DSL语言
  • 【STM32】HAL库——串口中断只接收到两个字符
  • 页面html结构导出为word或pdf