最近在練LeetCode題目,因為也有在學python 所以就想說把剛剛用c++解的題目用python寫寫看 一樣的algorithm拿去跑結果出現 "Time Limit Exceeded" 想請教一下為何這樣的寫法在python下performance會不好? 我用c++寫一樣的邏輯有通過 class Solution(object): def getSum(self, a, b): if (a&b) == 0: return a|b while (a&b) != 0: bit_add = a^b carry = (a&b) << 1 a = bit_add b = carry return a|b C++版 class Solution { public: int getSum(int a, int b) { if ((a&b) == 0) return a|b; while ((a&b) != 0) { int bit_add = a^b; int carry = (a&b) << 1; a = bit_add; b = carry; } return a|b; } };