Cha*_*oor 5 c++ optimization performance operators mod
我正在尝试解决以下问题:https : //leetcode.com/problems/add-digits/
以下方法耗时12ms完成所有测试:
int addDigits(int num) {
return 1+((num-1)%9);
}
Run Code Online (Sandbox Code Playgroud)
而以下仅用了8ms:
int addDigits(int num) {
return ((num-1)%9)+1;
}
Run Code Online (Sandbox Code Playgroud)
当我在末尾而不是开头加1时,为什么会有如此显着的差异?在计算时是否应该始终将常量放在最后?
| 归档时间: |
|
| 查看次数: |
104 次 |
| 最近记录: |