当前位置:网站首页>890. 查找和替换模式 / 剑指 Offer II 080. 含有 k 个元素的组合
890. 查找和替换模式 / 剑指 Offer II 080. 含有 k 个元素的组合
2022-06-12 16:31:00 【彼淇梁】
890. 查找和替换模式【中等题】【每日一题】
思路:【哈希表模拟】
见代码注释
代码:
class Solution {
public List<String> findAndReplacePattern(String[] words, String pattern) {
List<String> ans = new ArrayList<>();
//求出pattern模式的模板mode
String mode = make(pattern);
for (String word : words) {
//求出每个单词的模式,判断其是否与mode相等,相等则将word添加到ans列表中
if (mode.equals(make(word))){
ans.add(word);
}
}
return ans;
}
/** * 利用哈希表模拟出传入单词word的模式 * @param word 传入单词 * @return 返回传入单词的模式 */
public String make(String word){
StringBuilder sb = new StringBuilder();
//记录当前单词中出现的字母,及其对应的模式字母
Map<Character,Character> map = new HashMap<>();
char ch = 'A';
char[] chars = word.toCharArray();
//第一次遍历,找出单词中存在的字母,及其对应的模式字母
for (char c : chars) {
if (!map.containsKey(c)){
map.put(c,ch++);
}
}
//第二次遍历,根据map中记录的字母及其模式字母,模拟出当前单词对应的模式
for (char c : chars) {
sb.append(map.get(c));
}
return sb.toString();
}
}
剑指 Offer II 080. 含有 k 个元素的组合【中等题】
思路:【二进制模拟】
用二进制数来模拟从1到n这n个数,统计当前二进制数中1的个数,如果有k个1,说明我们找到了一个含k个元素的组合,将这个二进制数从右到左依次编号为1,2,...n,取出1对应的编号,将其添加到list集合中,最后将list集合添加进入ans答案列表中。
代码:
class Solution {
public List<List<Integer>> combine(int n, int k) {
List<List<Integer>> ans = new ArrayList<>();
int mask = 1 << n;
for (int i = 0; i < mask; i++) {
if (Integer.bitCount(i) == k){
List<Integer> list = new ArrayList<>();
int n1 = i,num = 1;
while (n1 != 0){
if ((n1 & 1) == 1){
list.add(num);
}
num++;
n1 >>= 1;
}
ans.add(new ArrayList<>(list));
}
}
return ans;
}
}
边栏推荐
- acwing 797 差分
- Analysis of Nacos config dynamic refresh source code
- acwing 803. Interval merging
- <山东大学项目实训>渲染引擎系统(六)
- The market share of packaged drinking water has been the first for eight consecutive years. How does this brand DTC continue to grow?
- Project training of Shandong University rendering engine system (II)
- std::set compare
- The C Programming Language(第 2 版) 笔记 / 7 输入与输出 / 7.8 其它函数
- acwing 2816. Judgement subsequence
- MYSQL---服务器配置相关问题
猜你喜欢

Thread pool execution process

收藏 | 22个短视频学习Adobe Illustrator论文图形编辑和排版

Batch --03---cmdutil

Batch --04--- moving components

【研究】英文论文阅读——英语poor的研究人员的福利

Gopher to rust hot eye grammar ranking

Statistical machine learning code set

The C Programming Language(第 2 版) 笔记 / 8 UNIX 系统接口 / 8.7 实例(存储分配程序)

<山东大学项目实训>渲染引擎系统(三)

批量--03---CmdUtil
随机推荐
大规模实时分位数计算——Quantile Sketches 简史
Comprendre le go des modules go. MOD et go. SUM
JS écoute si l'utilisateur allume le focus de l'écran
Interview: difference between '= =' and equals()
Interview: why do integer wrapper classes try to use equals() to compare sizes
收藏 | 22个短视频学习Adobe Illustrator论文图形编辑和排版
Batch --03---cmdutil
[research] reading English papers -- the welfare of researchers in English poor
Interview: do you understand the packing and unpacking operations?
CAS乐观锁
Analysis of Nacos config dynamic refresh source code
VIM from dislike to dependence (16) -- macro
acwing788. 逆序对的数量
MongoDB系列之SQL和NoSQL的区别
你的下一台电脑何必是电脑,探索不一样的远程操作
Kill program errors in the cradle with spotbugs
acwing 2816. Judgement subsequence
h t fad fdads
generate pivot data 1
generate pivot data 2