1812. 判断国际象棋棋盘中一个格子的颜色 - 力扣(LeetCode) (leetcode-cn.com)
一道规律题,可以发现横坐标与纵坐标加和为偶数则为白色,奇数则为黑色
//c
bool squareIsWhite(char * coordinates){
int x=coordinates[0]-'a';//ASCII码的关系,假设a为0
int y=coordinates[1]-'0';
if((x+y)%2==0)return true;
return false;
}
1812. 判断国际象棋棋盘中一个格子的颜色 - 力扣(LeetCode) (leetcode-cn.com)
//c++
class Solution {
public:
bool squareIsWhite(string coordinates) {
int x = coordinates[0] - 'a';//偶数为黑,奇数为白
int y = coordinates[1] - '0';
return ((x + y) % 2 == 0);
}
};
LCP 17. 速算机器人 - 力扣(LeetCode) (leetcode-cn.com)
这道题就是遍历数组,符合哪个就执行哪个
//c
int calculate(char* s){
int len=strlen(s);
int x=1,y=0;
for(int i=0;i<len;i++)
{
if(s[i]=='A')
{
x=2*x+y;
}
else if(s[i]=='B')
{
y=2*y+x;
}
}
return x+y;
}
//c++
class Solution {
public:
int calculate(string s) {
int x=1,y=0;
int len=s.size();
for(int i=0;i<len;i++)
{
if(s[i]=='A')
{
x=2*x+y;
}
else if(s[i]=='B')
{
y=2*y+x;
}
}
return x+y;
}
};
2011. 执行操作后的变量值 - 力扣(LeetCode) (leetcode-cn.com)
这道题遍历数组就可以
class Solution {
public:
int finalValueAfterOperations(vector<string>& operations) {
int x=0;
int len=operations.size();
for(int i=0;i<len;i++)
{
if(operations[i]=="++X"||operations[i]=="X++")
{
x++;
}
else
{
x--;
}
}
return x;
}
};