最近在练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; } };