当前位置:网站首页>890. find and replace mode / Sword finger offer II 080 Combination with k elements
890. find and replace mode / Sword finger offer II 080 Combination with k elements
2022-06-12 16:42:00 【Biqiliang】
890. Find and replace patterns 【 Medium question 】【 A daily topic 】
Ideas :【 Hash table emulation 】
See code Notes
Code :
class Solution {
public List<String> findAndReplacePattern(String[] words, String pattern) {
List<String> ans = new ArrayList<>();
// Find out pattern Templates for patterns mode
String mode = make(pattern);
for (String word : words) {
// Find out the pattern of each word , Determine whether it is consistent with mode equal , If it is equal, it will word Add to ans In the list
if (mode.equals(make(word))){
ans.add(word);
}
}
return ans;
}
/** * Use the hash table to simulate the incoming words word The pattern of * @param word Pass in the word * @return Returns the pattern of the incoming word */
public String make(String word){
StringBuilder sb = new StringBuilder();
// Record the letters that appear in the current word , And its corresponding mode letters
Map<Character,Character> map = new HashMap<>();
char ch = 'A';
char[] chars = word.toCharArray();
// First traversal , Find the letters in the word , And its corresponding mode letters
for (char c : chars) {
if (!map.containsKey(c)){
map.put(c,ch++);
}
}
// Second traversal , according to map The letters recorded in and their pattern letters , Simulate the pattern corresponding to the current word
for (char c : chars) {
sb.append(map.get(c));
}
return sb.toString();
}
}
The finger of the sword Offer II 080. contain k A combination of elements 【 Medium question 】
Ideas :【 Binary analog 】
Use binary numbers to simulate from 1 To n this n Number , Count the current binary number 1 The number of , If there is k individual 1, It means that we have found a k A combination of elements , Number the binary number from right to left 1,2,...n, Take out 1 The corresponding number , Add it to list Collection , The final will be list Add collection into ans In the answer list .
Code :
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;
}
}
边栏推荐
- The C programming language (version 2) notes / 8 UNIX system interface / 8.1 file descriptor
- generate pivot data 1
- Possible problems of long jump in gaussdb
- The process of "unmanned aquaculture"
- Joint recruitment notice of ganfei research group of Wuhan University and xuzhenjiang research group of Nanchang University
- 收藏 | 22个短视频学习Adobe Illustrator论文图形编辑和排版
- acwing 2816. Judgement subsequence
- 33-【go】Golang sync.WaitGroup的用法—保证go协程执行完毕,主协程才退出
- 你的下一台电脑何必是电脑,探索不一样的远程操作
- Comprendre le go des modules go. MOD et go. SUM
猜你喜欢

generate pivot data 0

'virtue and art' in the field of recurrent+transformer video recovery

Cookies and sessions

canvas 高级功能(下)

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

CVPR 2022 | meta learning performance in image regression task

Acwing788. number of reverse order pairs

acwing 790. The cubic root of a number (floating-point number in half)

Acwing 798 two dimensional difference (difference matrix)

Batch --03---cmdutil
随机推荐
《安富莱嵌入式周报》第268期:2022.05.30--2022.06.05
Acwing 797 differential
Joint recruitment notice of ganfei research group of Wuhan University and xuzhenjiang research group of Nanchang University
【BSP视频教程】BSP视频教程第17期:单片机bootloader专题,启动,跳转配置和调试下载的各种用法(2022-06-10)
h t fad fdads
Leetcode 2194. Cellules dans une plage dans un tableau Excel (OK, résolu)
Object. Keys traverses an object
Statistical machine learning code set
Exception assertion of assertj
34-【go】Golang channel知识点
\begin{algorithm} 笔记
[MySQL] internal connection, external connection and self connection (detailed explanation)
1.delete
MongoDB 学习整理(用户,数据库,集合,文档 的基础命令学习)
3/6 线性系统的时域分析法(上)
\begin{algorithm} 笔记
Nacos Config 动态刷新源码剖析
Canvas advanced functions (Part 2)
Project training of Shandong University rendering engine system (IV)
\begin{algorithm} 笔记