今日总结
- java随笔录------redis集群方案
- AI随探录------继智能输入法案例
- 代码随想录------复原IP地址,子集
目录
详细内容
java随笔录
redis集群有哪些方案
答:主从复制、哨兵模式、分片集群
主从复制:单节点reids的并发能力是有上线的,要进一步提高redis的并发能力,需要搭建主从集群,实现读写分离。例如让主节点进行写操作(完成后要数据同步给从节点),从节点进行读操作
主从数据同步的流程:(两种)
1、主从全量同步

如何判断是否第一次同步呢
根据replid进行判断,每个节点都有一个id,当id不一致时,说明他是第一次进行请求数据同步。执行上述顺序。如果一致,说明之前同步过,主节点就不会生成RDB文件了,他会通过repl_baklog文件进行后续的数据同步
那如何判断从repl_baklog文件读取多少数据呢
根据offset值进行判断,如下

2、主从增量同步(slave重启或后期数据变化)

AI随探录
继智能输入法案例
python
import torch
import config
from src.dataset import get_dataloader
from src.model import InputMethodModel
from src.predict import predict_batch
def evaluate(model, test_dataloader, device):
top1_acc_count = 0
top5_acc_count = 0
total_count = 0
#模型进入到评估模式
model.eval()
#不进行梯度计算
with torch.no_grad():
for inputs, targets in test_dataloader:
inputs, targets = inputs.to(device), targets.tolist()
top5_indexes_list = predict_batch(model, inputs)
for target, top5_index in zip(targets, top5_indexes_list):
total_count += 1
if target == top5_index[0]:
top1_acc_count += 1
if target in top5_index:
top5_acc_count += 1
return top1_acc_count / total_count, top5_acc_count / total_count
def run_evaluate():
# 1. 确定设备
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# 2.词表
with open(config.MODELS_DIR / 'vocab.txt', 'r', encoding='utf-8') as f:
vocab_list = [line.strip() for line in f.readlines()]
word2index = {word: index for index, word in enumerate(vocab_list)}
index2word = {index: word for index, word in enumerate(vocab_list)}
# 3.模型
model = InputMethodModel(vocab_size=len(vocab_list)).to(device)
model.load_state_dict(torch.load(config.MODELS_DIR / 'model.pt'))
#4,数据集
test_dataloader = get_dataloader(train=False)
#5评估逻辑
top1_acc, top5_acc = evaluate(model, test_dataloader, device)
print("评估结果")
print(f"top1_acc: {top1_acc}")
print(f"top5_acc: {top5_acc}")
if __name__ == '__main__':
run_evaluate()

代码随想录
复原IP地址
有效 IP 地址 正好由四个整数(每个整数位于
0到255之间组成,且不能含有前导0),整数之间用'.'分隔。
- 例如:
"0.1.2.201"和"192.168.1.1"是 有效 IP 地址,但是"0.011.255.245"、"192.168.1.312"和"192.168@1.1"是 无效 IP 地址。给定一个只包含数字的字符串
s,用以表示一个 IP 地址,返回所有可能的有效 IP 地址 ,这些地址可以通过在s中插入'.'来形成。你 不能 重新排序或删除s中的任何数字。你可以按 任何 顺序返回答案。示例 1:
输入:s = "25525511135" 输出:["255.255.11.135","255.255.111.35"]示例 2:
输入:s = "0000" 输出:["0.0.0.0"]示例 3:
输入:s = "101023" 输出:["1.0.10.23","1.0.102.3","10.1.0.23","10.10.2.3","101.0.2.3"]
java
class Solution {
List<String> result = new ArrayList<>();
StringBuilder stringBuilder = new StringBuilder();
private void back(String s, int ids, int number) {
if(ids == s.length() && number == 4) {
result.add(stringBuilder.toString());
return;
}
if(ids == s.length() || number == 4) {
return;
}
for(int i = ids; i < s.length() && i - ids < 3 && Integer.parseInt(s.substring(ids,i + 1)) >= 0 && Integer.parseInt(s.substring(ids,i + 1)) <= 255; i++) {
if(i - ids > 0 && s.charAt(ids) - '0' == 0) {
continue;
}
stringBuilder.append(s.substring(ids,i + 1));
if(number < 3)
stringBuilder.append(".");
back(s, i + 1, ++number);
number--;
stringBuilder.delete(ids + number, i + number + 2);
}
}
public List<String> restoreIpAddresses(String s) {
back(s,0,0);
return result;
}
}
子集
给你一个整数数组
nums,数组中的元素 互不相同 。返回该数组所有可能的子集(幂集)。解集 不能 包含重复的子集。你可以按 任意顺序 返回解集。
示例 1:
输入:nums = [1,2,3] 输出:[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]示例 2:
输入:nums = [0] 输出:[[],[0]]
java
class Solution {
List<List<Integer>> result = new ArrayList<>();
List<Integer> path = new LinkedList<>();
private void back(int[] nums, int ids) {
result.add(new ArrayList(path));
if(ids >= nums.length) {
return;
}
for(int i = ids; i < nums.length; i++) {
path.add(nums[i]);
back(nums, i + 1);
path.removeLast();
}
}
public List<List<Integer>> subsets(int[] nums) {
back(nums, 0);
return result;
}
}