时间轴
模拟
题目:
这个就是杨辉三角,这里我们直接模拟即可。用队列来实现,注意要区分第一个和最后一个,因为这两个数在加法并取余中只用到了一次。因此我们用-1 做为标识。表示-1 前面这个数字为最后一个数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| #include <string> #include <queue> using std::string; using std::queue;
class Solution { public: bool hasSameDigits(string s) { queue<int> que; int val1, val2; for (char ch : s) { que.push(ch - '0'); } que.push(-1);
while (que.size() > 3) { val1 = que.front(); que.pop();
val2 = que.front(); if (val2 == -1) { que.pop(); que.push(-1); continue; } que.push((val1 + val2) % 10); }
if (que.size() == 3) { val1 = que.front(); que.pop(); val2 = que.front(); if (val1 == val2) { return true; } } return false; } };
|