对于整数x,x % (10 ** 9 + 7)
并且x % (1e9 + 7)
都给人一种几次迭代后不同的结果。为了重现这个结果,我分享了我的LeetCode #576解决方案。越界路径。如果我更改return ans % (10 ** 9 + 7)
为return ans % (1e9 + 7)
(意识到这花了我一个小时),我的解决方案将不会通过 94 个测试用例中的 5 个。
请注意,此解决方案比此处某个天才家伙提出的单行解决方案要长得多。然而,同样的问题,他的解决方案出现,如果我们改变% (10 ** 9 + 7)
对% (1e9 + 7)
。
我玩了一下 python 编译器,注意到 1e9 给出了一个浮点文字。所以在我看来,这种特性是由浮点运算的“怪异”引起的。但我仍然不明白小数点后的零如何导致差异。为什么会出现这种差异?
无需复制,可以在此处找到差异:https : //www.diffchecker.com/PyKQCElB
要重现,这是我的解决方案:
class Solution:
def findPaths(self, m: int, n: int, maxMove: int, startRow: int, startColumn: int) …
Run Code Online (Sandbox Code Playgroud) 我在 C++ 中练习 lambda 函数,下面的代码工作正常
void insertionSort(int* a, int size, bool reverse=false) {
auto comp = [](int a, int b, bool reverse) {
return reverse ? a > b : b < a;
};
for (int i = 0; i < size; i++) {
int current = a[i];
cout << current <<endl;
int j = i-1;
while (j >= 0 && comp(current, a[j], reverse)) {
a[j+1] = a[j]; //shift right
j--;
}
a[j+1] = current;
}
show(a, size); //another function …
Run Code Online (Sandbox Code Playgroud)