当前位置:网站首页>Leetcode 2293. 极大极小游戏(可以.一次过)
Leetcode 2293. 极大极小游戏(可以.一次过)
2022-06-10 13:53:00 【我不是萧海哇~~~~】

给你一个下标从 0 开始的整数数组 nums ,其长度是 2 的幂。
对 nums 执行下述算法:
- 设 n 等于 nums 的长度,如果 n == 1 ,终止 算法过程。否则,创建 一个新的整数数组 newNums ,新数组长度为 n
/ 2 ,下标从 0 开始。 - 对于满足 0 <= i < n / 2 的每个 偶数 下标 i ,将 newNums[i] 赋值 为 min(nums[2 * i],
nums[2 * i + 1]) 。 - 对于满足 0 <= i < n / 2 的每个 奇数 下标 i ,将 newNums[i] 赋值 为 max(nums[2 * i],
nums[2 * i + 1]) 。 - 用 newNums 替换 nums 。
- 从步骤 1 开始 重复 整个过程。
执行算法后,返回 nums 中剩下的那个数字。
示例 1:

输入:nums = [1,3,5,2,4,8,2,2]
输出:1
解释:重复执行算法会得到下述数组。
第一轮:nums = [1,5,4,2]
第二轮:nums = [1,4]
第三轮:nums = [1]
1 是最后剩下的那个数字,返回 1 。
示例 2:
输入:nums = [3]
输出:3
解释:3 就是最后剩下的数字,返回 3 。
提示:
- 1 <= nums.length <= 1024
- 1 <= nums[i] <= 10^9
- nums.length 是 2 的幂
Code:
class Solution {
public:
int minMaxGame(vector<int>& nums) {
do
{
int n=nums.size();
if(n==1)
return nums[0];
vector<int>newnums;
newnums.resize(nums.size()/2);
copy(nums.begin(),nums.begin()+nums.size()/2,newnums.begin());
for(int i=0;i<newnums.size();i++)
{
if((i%2)==0)
{
int num=min(nums[2*i],nums[2*i+1]);
newnums[i]=num;
}
else
{
int num=max(nums[2*i],nums[2*i+1]);
newnums[i]=num;
}
}
nums=newnums;
}while(1);
return 0;
}
};
边栏推荐
- 新功能|Mail GPU Counter模块新增GPU图元处理和GPU Shader Cycles
- How to solve the problem that vmware tools are grayed out when VMware Workstation is installed
- What is the p value of a gene?
- 解决win10虚拟机和主机不能互相粘贴复制的问题
- buuctf [PHP]inclusion
- RecyclerView多布局写法,“我的”、“个人中心” 页面经典写法演示
- Leetcode 829. 连续整数求和
- 苹果生产线迁离,说明5G工业互联、智能制造对中国制造帮助有限
- What happened when the legendary login prompt failed to connect to the server? How to solve it?
- buuctf [PHP]XDebug RCE
猜你喜欢
![[deep learning 05] cross entropy loss function](/img/00/7bf13cb86324f1e9942ce45d5cc13c.png)
[deep learning 05] cross entropy loss function

What can the graph of training and verification indicators tell us in machine learning?

【深度学习05】 交叉熵损失函数

Win10 virtual machine download and installation process

传奇登录器提示连接服务器失败是怎么回事?怎么解决?

Im instant messaging development: the underlying principle of process killed and app skills to deal with killed
![buuctf [PHP]inclusion](/img/02/d328ed84e4641c09c5b1eba3ac6ea9.png)
buuctf [PHP]inclusion

《软件体系结构原理、方法与实践》第二版期末考试复习总结

Meetup review how Devops & mlops solve the machine learning dilemma in enterprises?

Qt: accessing controls in other forms
随机推荐
苹果生产线迁离,说明5G工业互联、智能制造对中国制造帮助有限
What can the graph of training and verification indicators tell us in machine learning?
[cloud computing] what is the relationship between a multi cloud management platform and a public cloud?
Textinputlayout usage details
Ten easy-to-use cross browser testing tools to share, good things worth collecting
大四应届毕业生,想自学软件测试,如何应对面试?
解决安装gerapy的时候报错:ERROR: Cannot uninstall ‘certifi‘. It is a distutils installed project...
Flutter 页面跳转 传参,TabBar学习总结5
【解决】每次加载已经训练好的模型,生成的向量会有不同
C#多线程学习笔记一
【抬杠C#】如何实现接口的base调用
Leetcode 829. Sum of continuous integers
[C language] pointer function, function pointer and array function
Kotlin 冒泡算法 ,高德地图 过滤掉两点之间距离小于50的数据,不重复显示
【专题介绍】圆桌论坛——AI与音视频技术的融合之路
The relocation of Apple's production line shows that 5g industrial interconnection and intelligent manufacturing have limited help for manufacturing in China
[notes] some records of 74hc573
CentOS Linux 已死!Oracle Linux 可能是它的更好替代品
如何定位游戏发热问题
【操作教程】如何正确使用海康demo工具配置通道上线?