当前位置:网站首页>LeetCode_22_Apr_4th_Week
LeetCode_22_Apr_4th_Week
2022-08-04 06:29:00 【KuoGavin】
It's really unpredictable,If something happened to the relatives in the middle, there is no energy to continue to brush the questions.Fortunately, this time node can take more care of her,The situation has now stabilized,The environment is almost configured,Pick it up again.I wish you all good health,生活如意!
April 25th : 398. 随机数索引
April 26th : 883. 三维形体投影面积
April 25th : 398. 随机数索引
Mainly the setting and use of random seeds:
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;
};
Comment There is a reservoir principle here,挺有意思的,The efficiency is also higher than the above method
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. 三维形体投影面积
The problem is that the area is ,The sum of the maximum values for each column+The sum of the maximum values for each row+非0行列数目,解题代码如下:
class Solution {
public:
int projectionArea(vector<vector<int>>& grid) {
int overLookView = 0, leftLookView = 0, rightLookView = 0;
int rowMax = 0, colMax = 0; //Traversal of multiplexed rows and columns,只需交换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;
}
};
边栏推荐
- 如何用Pygame制作简单的贪吃蛇游戏
- 强化学习中,Q-Learning与Sarsa的差别有多大?
- No matching function for call to 'RCTBridgeModuleNameForClass'
- FAREWARE ADDRESS
- target has libraries with conflicting names: libcrypto.a and libssl.a.
- LeetCode_Dec_2nd_Week
- 抽象类、内部类和接口
- 卷积神经网络入门详解
- 度量学习(Metric learning)—— 基于分类损失函数(softmax、交叉熵、cosface、arcface)
- PyTorch
猜你喜欢
AWS uses EC2 to reduce the training cost of DeepRacer: DeepRacer-for-cloud practical operation
语音驱动嘴型与面部动画生成的现状和趋势
多层LSTM
MOOSE平台官方第二个例子分析——关于创建Kernel,求解对流扩散方程
No matching function for call to ‘RCTBridgeModuleNameForClass‘
tensorRT5.15 使用中的注意点
管道重定向
腾讯、网易纷纷出手,火到出圈的元宇宙到底是个啥?
Copy Siege Lion 5-minute online experience MindIR format model generation
MFC读取点云,只能正常显示第一个,显示后面时报错
随机推荐
迅雷关闭自动更新
LeetCode_Dec_1st_Week
光条中心提取方法总结(二)
Comparison of oracle's number and postgresql's numeric
Endnote编辑参考文献
latex-写论文时一些常用设置
亚马逊云科技Build On-Amazon Neptune基于知识图谱的推荐模型构建心得
tensorRT5.15 使用中的注意点
PCL窗口操作
第三章 标准单元库(下)
(Navigation page) OpenStack-M version - manual construction of two nodes - with video from station B
Copy攻城狮的年度之“战”|回顾2020
SFTP的用法
target has libraries with conflicting names: libcrypto.a and libssl.a.
fuser 使用—— YOLOV5内存溢出——kill nvidai-smi 无pid 的 GPU 进程
FAREWARE ADDRESS
浅谈游戏音效测试点
MNIST Handwritten Digit Recognition - Building a Perceptron from Zero for Two-Classification
安装pyspider后运行pyspider all后遇到的问题
Copy Siege Lion's Annual "Battle" | Review 2020