当前位置:网站首页>LeetCode_22_Apr_4th_Week
LeetCode_22_Apr_4th_Week
2022-08-04 05:30:00 【KuoGavin】
真真是天有不测风云,中间亲人出了点事情就没有精力继续刷题了。万幸是这个时间节点还可以多照顾她,现在情况也稳定下来了,环境也差不多配置好了,再捡回来好了。祝大家都健健康康,生活如意!
April 25th : 398. 随机数索引
April 26th : 883. 三维形体投影面积
April 25th : 398. 随机数索引
主要是随机种子的设置和使用:
srand((unsigned)time(NULL));
rand()%num //等概率获取0~num-1之间的任一数字
解题代码如下:
class Solution {
public:
Solution(vector<int>& nums) {
srand((unsigned)time(NULL));
for(int i = 0; i < nums.size(); ++i) keys[nums[i]].push_back(i);
}
int pick(int target) {
return keys[target][rand()%keys[target].size()];
}
private:
unordered_map<int, vector<int>> keys;
};
评论这里有个蓄水池原理,挺有意思的,效率也比上述方法更高
class Solution {
public:
vector<int> res;
Solution(vector<int>& nums) {
res = nums;
}
int pick(int target) {
int c = 0, index = 0;
for(int i = 0;i < res.size();i++)
if(res[i] == target){
c++;
if(rand() % c == 0) index = i;
}
return index;
}
};
April 26th :April 26th : 883. 三维形体投影面积
该题就是面积为,每列最大值的和+每行最大值的和+非0行列数目,解题代码如下:
class Solution {
public:
int projectionArea(vector<vector<int>>& grid) {
int overLookView = 0, leftLookView = 0, rightLookView = 0;
int rowMax = 0, colMax = 0; //复用行列的遍历,只需交换i,j的位置即可
for(int i = 0; i < grid.size(); ++i) {
rowMax = 0, colMax = 0;
for(int j = 0; j < grid[i].size(); ++j) {
overLookView += grid[i][j] ? 1 : 0;
rowMax = max(grid[i][j], rowMax);
colMax = max(grid[j][i], colMax);
}
leftLookView += rowMax;
rightLookView += colMax;
}
return overLookView + leftLookView + rightLookView;
}
};
边栏推荐
猜你喜欢
makefile基础学习
Attention Is All You Need(Transformer)
Qt日常学习
【Copy攻城狮日志】飞浆学院强化学习7日打卡营-学习笔记
Copy Siege Lion 5-minute online experience MindIR format model generation
fuser 使用—— YOLOV5内存溢出——kill nvidai-smi 无pid 的 GPU 进程
MNIST Handwritten Digit Recognition - Building a Perceptron from Zero for Two-Classification
DeblurGAN-v2: Deblurring (Orders-of-Magnitude) Faster and Better 图像去模糊
TensorFlow2 study notes: 7. Optimizer
在AWS-EC2中安装Minikube集群
随机推荐
学习资料re-id
fuser 使用—— YOLOV5内存溢出——kill nvidai-smi 无pid 的 GPU 进程
MNIST Handwritten Digit Recognition - Building a Perceptron from Zero for Two-Classification
MAE 论文《Masked Autoencoders Are Scalable Vision Learners》
[Deep Learning 21 Days Learning Challenge] Memo: What does our neural network model look like? - detailed explanation of model.summary()
[CV-Learning] Convolutional Neural Network Preliminary Knowledge
TensorRT 5 初步认识
基于BiGRU和GAN的数据生成方法
卷积神经网络入门详解
TypeError: load() missing 1 required positional argument: ‘Loader‘
MNIST手写数字识别 —— 基于Mindspore快速构建感知机实现十分类
Install Minikube Cluster in AWS-EC2
lstm pipeline 过程理解(输入输出)
打金?工作室?账号被封?游戏灰黑产离我们有多近
语音驱动嘴型与面部动画生成的现状和趋势
光条提取中的连通域筛除
Attention Is All You Need(Transformer)
Amazon Cloud Technology Build On-Amazon Neptune's Knowledge Graph-Based Recommendation Model Building Experience
PostgreSQL schema (Schema)
亚马逊云科技 Build On 2022 - AIot 第二季物联网专场实验心得