题目来源于知识星球—英雄算法联盟,六月算法集训专题
目录
前言
跟随英雄算法联盟博主—英雄哪里出来,每天完成相应的算法练习,一个月后,必定会有所成长!
一、2059.转化数字的最小运算数(中等)
1.题目描述
2.解题思路
看题解敲得,纯广搜模板,每次遍历数组,入队,扩展状态。
3.代码演示(C++)
#define maxn 1010
class Solution
{
public:
int minimumOperations(vector<int>& nums, int start, int goal)
{
queue<int> q;
int step[maxn];
int x, nextx, i;
memset(step, -1, sizeof(step));
q.push(start);
step[start] = 0;
while(!q.empty())
{
x = q.front();
q.pop();
for(i = 0; i < nums.size(); ++i)
{
nextx = x + nums[i];
if(nextx == goal)
{
return step[x] + 1;
}
if(nextx <= 1000 && nextx >= 0)
{
if(step[nextx] == -1)
{
step[nextx] = step[x] + 1;
q.push(nextx);
}
}
nextx = x - nums[i];
if(nextx == goal)
{
return step[x] + 1;
}
if(nextx <= 1000 && nextx >= 0)
{
if(step[nextx] == -1)
{
step[nextx] = step[x] + 1;
q.push(nextx);
}
}
nextx = ( x ^ nums[i] );
if(nextx == goal)
{
return step[x] + 1;
}
if(nextx <= 1000 && nextx >= 0)
{
if(step[nextx] == -1)
{
step[nextx] = step[x] + 1;
q.push(nextx);
}
}
}
}
return -1;
}
};
4.题目链接
二、690.员工的重要性(中等)
1.题目描述
2.解题思路
下次再战!
3.代码演示(C++)
4.题目链接
三、672.灯泡开关 II(中等)
1.题目描述
2.解题思路
下次再战!
3.代码演示(C++)
4.题目链接
总结
每天跟随英雄哥学习相关的算法,一个月会收获很多,如果你想了解更多关于知识星球的内容,欢迎联系我!