今天打代码打的太急,力扣抛出异常,来看一看
就是这个,代码也很容易看懂
class Solution {
public:
int lastRemaining(int n, int m) {
bool flag = false;
vector<char>v;
for (int i = 0; i < n; i++) {
v.push_back(true);
}
int i = 0, count = 0;
while (!flag)
{
if (v[i] == false) {
i ++;
continue;
}
if (i == v.size() - 1) {
i = 0; <------
}
if (i % m == 0) { <------
v[i] == false; <------报错
count ++;
}
if (count == m - 1)
flag = true;
i++;
}
for (int i = 0; i < v.size(); i++)
if (v[i])
return v[i];
}
};
这个代码中的三个箭头,if中是为了判断是否相等,说白了是一种判断关系,叫做关系运算符,用双等号==,而赋值是为了把右值赋给固定的左值,所以用等号=。叫做赋值运算符。
代码应该改成这样
class Solution {
public:
int lastRemaining(int n, int m) {
bool flag = false;
vector<char>v;
for (int i = 0; i < n; i++) {
v.push_back(true);
}
int i = 0, count = 0;
while (!flag)
{
if (v[i] == false) {
i ++;
continue;
}
if (i == v.size() - 1) {
i = 0;
}
if (i % m == 0) {
v[i] = false;
count ++;
}
if (count == m - 1)
flag = true;
i++;
}
for (int i = 0; i < v.size(); i++)
if (v[i])
return v[i];
}
};