371. Sum of two integers [easy] (Python) __python

Source: Internet
Author: User
Tags bitwise
Topic link

The original title of https://leetcode.com/problems/sum-of-two-integers/

Calculate the sum of two integers a and B, but you are don't allowed to use the operator + and-.

Example:
Given A = 1 and b = 2, return 3. Title Translation

Calculates the and of two integers a and B, but cannot use operator plus and minus signs.
For example: Given a=1,b=2, return 3. method of Thinking

Since you cannot use addition and subtraction, use a bitwise operation. The following example of computational 5+4 illustrates how to add a bitwise operation to an addition:
1. Use binary to express two addends, a=5=0101,b=4=0100;
2. With and (&) operation to get all the bits on the carry carry=0100;
3. Use XOR (^) operation to find A and B different bits, assign value to a,a=0001;
4. Move the carry carry left one position, assign value to b,b=1000;
5. Loop until the carry carry is 0, this time get a=1001, that is, the last sum.

It's a normal idea, but it's a bit of a hassle for python. Because Python's integer is not fixed 32 bits, so need to do some special processing, see the code specifically.
The code takes a number to 0x100000000 (note: Python's modulo operation results in a non-negative number, which is the hope that the binary representation of the numbers from the 32nd bit to the higher bit is 0 (the lowest bit is the No. 0) to simulate a 32-bit int on the 0-31-bit. idea of a

Iterative solution.

Code

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

Recursive solution.

Code

Class Solution (object):
    def getsum (self, A, b): ""
        : Type a:int
        : Type b:int
        : Rtype:int "" "
        tmp_a = Self.add (A, b) return
        tmp_a if tmp_a <= 0x7fffffff Else tmp_a | (~0x100000000+1)

    def add (self, A, b): Return
        a If B = 0 Else Self.add ((a ^ b)% 0x100000000, ((A & B) << 1)% 0x100000000)

PS: Novice Brush Leetcode, new handwritten blog, write wrong or write is not clear also please help point out, thank you.
Reprint Please note: http://blog.csdn.net/coder_orz/article/details/52034541

Related Article

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.