cherrychenlee

导航

 

原文地址:https://www.jianshu.com/p/1a6db2f5bb64

时间限制:1秒 空间限制:32768K

题目描述

写一个函数,求两个整数之和,要求在函数体内不得使用+、-、*、/四则运算符号。

我的代码

class Solution {
public:
    int Add(int num1, int num2)
    {
        while(num2){
            int tmp=num1^num2;//按位加,无进位
            num2=(num1&num2)<<1;//进位
            num1=tmp;
        }
        return num1;
    }
};

运行时间:5ms
占用内存:596k

posted on 2019-05-07 13:04  cherrychenlee  阅读(107)  评论(0编辑  收藏  举报