ソードフィンガーオファーインタビュー質問65.足し算、引き算、掛け算、割り算の必要はありません[簡単]

https://leetcode-cn.com/problems/bu-yong-jia-jian-cheng-chu-zuo-jia-fa-lcof/solution/mian-shi-ti-65-bu-yong-jia-jian-チェンチュズオジ-7 /

ビット演算を理解していない場合は、覚えておいてください

class Solution {
public:
    int add(int a, int b) {
        int c=0;
        while(b){
            c=(unsigned int)(a&b)<<1;
            a^=b;
            b=c;
        }
        return a;
    }
};

 

おすすめ

転載: blog.csdn.net/qq_41041762/article/details/105898020