leetcode--371. Sum of two integers (two integers and) Python__python

Source: Internet
Author: User
Tags bitwise

Topic:

Given two integers a and B, it is required not to use ' + ' and '-' to return the and of the two integers.

Ideas for solving problems:

Because the subtraction symbol cannot be used, it is considered that the bitwise-and-bitwise-OR-bitwise operation of the binary number is known to be a^b or can be obtained without taking into account the result of the rounding, bitwise and a&b of the case where the rounding can be obtained. So the result can be obtained by adding the results of the rounding to the left one and then the result of the addition of the different or. The rounding result, when added to the XOR or result, may also produce a carry. Therefore, loop the above steps until the result of the carry is terminated at 0 o'clock.

Code (Python):

Class Solution (object):
    def getsum (self, A, b): ""
        : Type a:int
        : Type b:int
        : Rtype:int
        "" C6/>while b!= 0:
            carry = a & B =
            (a ^ b)
            B = (carry << 1) return 
        A If a <= 0x7fffffff els E a| (~0x100000000+1)


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.