An instinctively solution with c++


  • 0
    W
    class Solution {
    	int getBit(int val, int pos) {
    		return (val >> pos) & 1;
    	}
    	int setBit(int val, int pos, int bitval) {
    		return val | (bitval << pos);
    	}
    public:
    	int getSum(int a, int b) {
    		int res = 0;
    		auto carry = 0;
    		auto bitcnt = sizeof(int) * 8;
    		for (auto pos = 0; pos < bitcnt; ++pos) {
    			auto bita = getBit(a, pos);
    			auto bitb = getBit(b, pos);
    			if (bita == 0 && bitb == 0) {
    				if (carry) {
    					res = setBit(res, pos, carry);
    					carry = 0;
    				}
    			}
    			else if (bita == 1 && bitb == 1) {
    				if (carry == 0) {
    					carry = 1;
    				}
    				else {
    					res = setBit(res, pos, carry);
    				}
    			}
    			else {
    				if (carry == 0) {
    					res = setBit(res, pos, 1);
    				}
    			}
    		}
    		return res;
    	}
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.