日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

leetcode 1178. Number of Valid Words for Each Puzzle | 1178. 猜字谜(bitmask位运算)

發(fā)布時(shí)間:2024/2/28 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 leetcode 1178. Number of Valid Words for Each Puzzle | 1178. 猜字谜(bitmask位运算) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題目

https://leetcode.com/problems/number-of-valid-words-for-each-puzzle/

題解

看了答案,堪稱力扣最詳細(xì)的答案,從時(shí)間復(fù)雜度的角度分析本題解法:

https://leetcode.com/problems/number-of-valid-words-for-each-puzzle/solution/

其中,有一個(gè)重要的 trick(不是很懂,但很實(shí)用)

Here we find another challenge: How to iterate over all the subsets of a set? This is a classic problem that can be solved using a depth-first search.

However, when working with a bitmask that represents a set of all possible items, we can use a simple trick to find all possible subsets of those items using only a for loop.

Let’s have a quick look at how this works. The pseudo-code is as follows:

for (subset = bitmask; subsets >= 0; subset = (subset - 1) & bitmask) {// do what you want with the current subset... }

Or with a while loop:

let subset = bitmask; while (subsets >= 0) {// do what you want with the current subset...subset = (subset - 1) & bitmask; }

Why does this work? The subsets must be included in range [0, bitmask], and if we iterate from bitmask to 0 one by one, we are guaranteed to visit the bitmask of every subset along the way.

But we can also meet those that are not a subset of bitmask. Fortunately, instead of decrementing subset by one at each iteration, we can use subset = (subset - 1) & bitmask to ensure that each subset only contains characters that exist in bitmask.

Also, we will not miss any subset because subset - 1 turns at most one 1 into 0.

代碼

class Solution {public List<Integer> findNumOfValidWords(String[] words, String[] puzzles) {// 對(duì)于每一個(gè)puzzle,求滿足條件的word的個(gè)數(shù)。// 條件:// 1. puzzle包含word的所有字母// 2. word包含puzzle的第一個(gè)字母Map<Integer, Integer> map = new HashMap<>();for (String word : words) {int bitmask = 0;for (int i = 0; i < word.length(); i++) {bitmask |= 1 << word.charAt(i) - 'a';}map.put(bitmask, map.getOrDefault(bitmask, 0) + 1);}List<Integer> result = new ArrayList<>();for (String puzzle : puzzles) {int bitmask = 0;for (int i = 0; i < puzzle.length(); i++) { bitmask |= 1 << puzzle.charAt(i) - 'a';}int first = 1 << puzzle.charAt(0) - 'a';int count = map.getOrDefault(first, 0);bitmask &= ~first;// all subsets of bitmask, trickfor (int subMask = bitmask; subMask > 0; subMask = (subMask - 1) & bitmask) {count += map.getOrDefault(subMask | first, 0);}result.add(count);}return result;} }

總結(jié)

以上是生活随笔為你收集整理的leetcode 1178. Number of Valid Words for Each Puzzle | 1178. 猜字谜(bitmask位运算)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。