【Lintcode】1786. Pub Sub Pattern
題目地址:
https://www.lintcode.com/problem/pub-sub-pattern/description
要求實現一個釋出/訂閱模式。實現下面功能:
1、subscribe(channel, user_id)
:將給定使用者訂閱到給定頻道。
2、unsubscribe(channel, user_id)
:取消訂閱給定使用者的給定使用者。
3、publish(channel, message)
:您需要將訊息釋出到頻道,以便在頻道上訂閱的每個人都會收到此訊息。 呼叫PushNotification.notify(user_id, message)
將訊息推送給使用者。
思路是雜湊表,key是channel名,value是個HashSet,存訂閱這個channel的user_id。程式碼如下:
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class PubSubPattern {
private Map<String, Set<Integer>> map;
public PubSubPattern() {
// Write your code here
map = new HashMap<>();
}
/**
* @param channel: the channel's name
* @param user_id: the user who subscribes the channel
* @return: nothing
*/
public void subscribe(String channel, int user_id) {
// Write your code here
map.putIfAbsent(channel, new HashSet<>());
map.get(channel).add(user_id);
}
/**
* @param channel: the channel's name
* @param user_id: the user who unsubscribes the channel
* @return: nothing
*/
public void unsubscribe(String channel, int user_id) {
// Write your code here
Set<Integer> set = map.get(channel);
if (set != null && !set.isEmpty()) {
set.remove(user_id);
}
}
/**
* @param channel: the channel's name
* @param message: the message need to be delivered to the channel's subscribers
* @return: nothing
*/
public void publish(String channel, String message) {
// Write your code here
Set<Integer> set = map.get(channel);
if (set != null && !set.isEmpty()) {
for (int user_id : set) {
PushNotification.notify(user_id, message);
}
}
}
}
class PushNotification {
public static void notify(int user_id, String the_message) {}
}
所有操作時間複雜度 O ( 1 ) O(1) O(1),空間與具體所有頻道共多少人訂閱有關。
相關文章
- 【Lintcode】1856. Sub-palindrome
- Redis的Pub/Sub客戶端實現Redis客戶端
- SpringBoot Redis 釋出訂閱模式 Pub/SubSpring BootRedis模式
- 使用 EMQX Cloud 橋接資料到 GCP Pub/SubMQCloud橋接GC
- 不使用 MQ 如何實現 pub/sub 場景?MQ
- redis原始碼分析之釋出訂閱(pub/sub)Redis原始碼
- 基於Pub/Sub模式的阿里雲IoT同步呼叫詳解模式阿里
- Spotify如何從Apache kafka遷移到雲平臺的pub/sub系統ApacheKafka
- Akka-Cluster(2)- distributed pub/sub mechanism 分散式釋出/訂閱機制分散式
- Redis 中使用 list,streams,pub/sub 幾種方式實現訊息佇列Redis佇列
- How to add sub-repo as sub-module
- 解釋一下這兩行 "pub": "pnpm --filter "./packages/*" run pub", "pub:beta": "pnpm --filter "./packages/*" run pub:beta"NPMFilterPackage
- pub.jsJS
- HTML <sub> 下標HTML
- SVG <pattern>SVG
- Adapter PatternAPT
- [LintCode] Daily TemperaturesAI
- [LintCode] Permutation in String
- Pegging a sub strapon in Shanghai/Suzhou/WuxiAIUX
- Cache Aside PatternIDE
- STL and Design Pattern
- [LintCode/LeetCode] Meeting RoomsLeetCodeOOM
- Lintcode 1263. Is Subsequence
- 【Lintcode】1189. Minesweeper
- 蜆子(Shell sub,Son clam,Clams)
- Reliable Key Holder for Chastity Sub Anywhere in the WorldAST
- 代理模式(Proxy Pattern)模式
- [review]Design Pattern:CommandView
- 策略模式【Strategy Pattern】模式
- [LeetCode/LintCode] Largest Palindrome ProductLeetCode
- [LintCode/LeetCode] Contains Duplicate IIILeetCodeAI
- [LintCode] Check Full Binary Tree
- [LintCode/LeetCode] Remove Duplicate LettersLeetCodeREM
- [LintCode] 3Sum Smaller
- 【Lintcode】1615. The Result of Investment
- [LintCode] Binary Tree Level Order
- 【Lintcode】1736. Throw Garbage
- 【Lintcode】1665. Calculate Number