zoukankan      html  css  js  c++  java
  • A + B 问题


    =================================================================================
    题目描述:
    =========
    描述
    中文
    English
    Write a function that add two numbers A and B.

    There is no need to read data from standard input stream.
    Both parameters are given in function aplusb, you job is to calculate the sum and return it.

    您在真实的面试中是否遇到过这个题?
    说明
    Are a and b both 32-bit integers?

    Yes.
    Can I use bit operation?

    Sure you can.
    样例
    Example 1:

    Input: a = 1, b = 2
    Output: 3
    Explanation: return the result of a + b.
    Example 2:

    Input: a = -1, b = 1
    Output: 0
    Explanation: return the result of a + b.
    挑战
    Of course you can just return a + b to get accepted. But Can you challenge not do it like that?(You should not use + or any arithmetic operators.)

    =================================================================================


    =================================================================================
    大神代码:
    ==========

    按位异或=不进位加法
    class Solution {  
    public:  
        / 
          @param a: An integer 
          @param b: An integer 
          @return: The sum of a and b  
         /  
        int aplusb(int a, int b) {  
            int result=a^b,carry=(a&b)<<1;  
            while(carry!=0){  
                a=result^carry;  
                carry=(result&carry)<<1;  
                result=a;  
            }  
            return result;  
        }  
    };  
    
    
     //下面这个算法超时:
     //re=a^b 不进位加法
       
     //jw=(a&b)<<1 左移动一位,然后和 re 这个结果进行 re^jw  不进位加法,(re&jw)<<1  直到进位jw=0 
    
    class Solution {  
    public:  
        / 
          @param a: An integer 
          @param b: An integer 
          @return: The sum of a and b  
         /  
        int aplusb(int a, int b) {  
            return b==0?a:aplusb(a^b,(a&b)<<1);  
        }  
    };

    =================================================================================


    =================================================================================
    大神解释:
    =========
    不用a+b的做法:
    这道题考察的应该就是底层二进制是如何实现数字的加减的。
    首先考虑把十进制数转化成二进制数。举例说明,譬如3+6;
    3-> 0000 0011
    6-> 0000 0110
    按位操作运算中,没有涉及进位的直接运算符。因此考虑结合


    a^b(不进位加法)
    (a&b)<<1(用于表示进位的位置)


    我们考虑我们在做加法的时候,是不是先把两个数按照不进位的加法进行运算(a^b),
    然后再在进位的位置加1((a&b)<<1).

    所以我理解,a&b)<<1这一步就像扫描一样,

    从最低位向最高位进行扫描,直到没有进位为止((a&b)<<1的结果为0)

    具体我们看这个实例:
    1.(a)3-> 0000 0011
    (b)6-> 0000 0110
    2.a^b= 0000 0101
    (a&b<<1)0000 0100 (恰好是第二位由于1^1造成的进位,导致第三位要加1,那么由于这个+1会不会造成第四位的进位呢?所以还需要递归下去)
    3. a^b= 0000 0001
    (a&b<<1)0000 1000
    4.a^b= 0000 1001
    (a&b<<1)0000 0000
    至此,递归结束了,因为第二项为0了,所以最终结果就是 0000 1001 转化为十进制为9
    over。


    =================================================================================

    =================================================================================
    我的代码:
    ==========

    #include<bitset>
    class Solution {
    public:
        /**
         * @param a: An integer
         * @param b: An integer
         * @return: The sum of a and b 
         */
        int aplusb(int a, int b) {
            // write your code here
    
        string str1;
        string str2;
        stringstream ss;
        ss.clear();
        ss<<(bitset<64>)a;
        ss>>str1;
        ss.clear();
        ss<<(bitset<64>)b;
        ss>>str2;
        int sum[129];
        sum[64]=0;
        int sumy[129];
        int len=64;
        for(int i=len-1,j=0; i>=0; i--,j++)
            sum[j]=str1[i]-'0';
        for(int i=len-1,j=0; i>=0; i--,j++)
            sumy[j]=str2[i]-'0';
        for (int i = 0; i < len; i++)
        {
            sum[i] += sumy[i];
            if (sum[i] >=2)
            {
                sum[i]%=2;
                sum[i+1]++;
            }
        }
        if(sum[len]!=0)
            len++;
    
        int da=1;
        int Sum=0;
        for(int i=len-1; i>=0; i--)
            if(sum[i]!=0)
            {
                len=i;
                break;
            }
        for(int i=0; i<=len; i++)
        {
            if(sum[i]&1)
                Sum+=da;
            da*=2;
        }
        return Sum;
    
        }
    };

    =================================================================================


    =================================================================================
    我的解释:
    =========
    我用了 大数加法,数据转换;

    数据到二进制,然后大数加法,然后再转到二进制
    =================================================================================

    =================================================================================
    其他:
    ======
    Python
    class Solution:
    def aplusb(self, a, b):
    mx, mask = 0x7FFFFFFF, 0xFFFFFFFF
    while b:
    a, b = (a ^ b) & mask, ((a & b) << 1) & mask
    return a if a <= mx else ~(a ^ mx)
    Javascript
    const aplusb = function (a, b) {
    while (b) {
    [a, b] = [a ^ b, (a & b) << 1];
    }
    return a;
    }
    =================================================================================

  • 相关阅读:
    动态表单功能
    IDEA2019版Run Dashboard调出方案
    js页面传递参数为中文乱码问题解决方法
    layui 一行多列控件时使用table布局
    npm 安装包失败 --- 清除npm缓存
    解析数据库连接字符串 (将Data Source、Initial Catalog、User ID、Password取出)
    SQL SERVER 存储过程语法
    mvc5 跨域访问
    钟表
    MVC session过期如何处理跳转(转)
  • 原文地址:https://www.cnblogs.com/NirobertEinteson/p/10946924.html
Copyright © 2011-2022 走看看