2016-09-22 32 views
1
class Solution(object): 
    def getSum(self, a, b): 
     if (a == 0): 
      return b 
     if (b == 0): 
      return a; 
     while(b != 0): 
      _a = a^b 
      _b = (a & b) << 1 
      a = _a 
      b = _b 
     return a 

但是當其中一個或兩個腳本應該如何?在Python 2或3中不使用'+'運算符的兩個整數之和

+2

爲什麼你要做這樣的事情呢? –

+0

這是一個leetcode問題,但我無法獲得正確的代碼。 (悲傷的臉) –

+4

爲什麼不只是'返回一個 - -b'? –

回答

3

+運營商內部使得在Python中唯一的工作致電__add__()。所以,您可以直接撥打a.__add__(b)來獲得金額。下面是修改後的代碼:

>>> class Solution(object): 
...  def getSum(self, a, b): 
...   return a.__add__(b) 
... 
>>> s = Solution() 
>>> s.getSum(1, 2) 
3 

OR,你可以使用operator.add(a, b)爲:

>>> import operator 
>>> operator.add(1, 2) 
3 
0

我喜歡解決別人的問題。

class Solution(object): 
    def getSum(self, a, b): 
     return sum([a, b]) 

編輯:如果你是在一些幻想。你可以做運算符重載。

class CrappyInt(int): 

    def __init__(self, num): 
     self.num = num 

    def __eq__(self, a): 
     return self.__add__(a) 


m = CrappyInt(3) 
n = CrappyInt(4) 

print m == n 
# 7 

這對你來說可能有點困難。但它很有趣。

+0

當它!我太親密了... –

+0

oops。有用。但我仍然想解決這個問題。 –

+0

我從來沒有想過會這麼容易。感謝你所做的一切。 –

0

您還可以使用operator

import operator 


class Solution(object): 
    def getSum(self, a, b): 
     return operator.add(a, b) 

如果你想用二進制位運算來麻煩。這是第一種方法:

def complex_add_1(x, y): 
    while y != 0: 
     b = x & y 
     x = x^y 
     y = b << 1 
    return x 

這裏是另外一個使用遞歸:

def complex_add_2(x, y): 
    if y == 0: 
     return x 
    else: 
     return complex_add_2(x^y, (x & y) << 1) 

編輯:爲位操作方法3

+0

我不認爲你的位智能函數適用於負數 – galaxyan

+0

@galaxyan他們在Python 3中工作,只是檢查。 –

+0

@ alex.l我已經添加了兩個更多的方法與按位操作(似乎你在找什麼) –

0

我的老高中的程序,我在我的USB記憶棒中。是的,它很粗糙,但它的工作原理。

def s(a,b): 
    a = bin(a)[2:] 
    b = bin(b)[2:] 
    c_in = 0 
    value = '' 
    if not len(a) == len(b): 
     to_fill = abs(len(a) - len(b)) 

     if len(a) > len(b): 
      b = b.zfill(len(a)) 
     else: 
      a = a.zfill(len(b)) 

    for i,j in zip(reversed(a),reversed(b)): 
     i_xor_j = int(i)^int(j) 
     i_and_j = int(i) and int(j) 
     s = int(c_in)^int(i_xor_j) 
     c_in_and_i_xor_j = int(c_in) and int(i_xor_j) 
     c_in = int(i_and_j) or int(c_in_and_i_xor_j) 
     value += str(int(s)) 
    value += str(int(c_in)) 

    return int(value[::-1],2) 

print(s(5,9)) 
#>> 14 

或者我會看看我是否可以用sum作弊。

+0

@MarkRansom我希望這讓你感到自豪。 – MooingRawr

0

這還挺有趣:)我不知道這是否是你追求的,但只要這些都應該按照您正在使用整數

def subtract(a, b): 
    if a < 0: 
     return negative(add(negative(a), b)) 
    if b < 0: 
     return add(a, negative(b)) 
    if a < b: 
     return negative(subtract(b, a)) 
    if b == 0: 
     return a 
    return subtract(a^b, (~a & b) << 1) 

def negative(a): 
    if a == 0: return 0 
    a = ~a 
    b = 1 
    while b > 0: 
     a, b = a^b, (a&b) << 1 
    return a 

def add(a, b): 
    if a < 0: 
     return negative(subtract(negative(a), b)) 
    if b < 0: 
     return subtract(a, negative(b)) 
    if b == 0: 
     return a 
    return add(a^b, (a & b) << 1) 

def multiply(a, b): 
    if a == 0 or b == 0: 
     return 0 
    if b < 0: 
     a = negative(a) 
     b = negative(b) 
    A = 0 
    while b > 0: 
     A = add(A, a) 
     b = subtract(b, 1) 
    return A 

def div(a, b): 
    if b == 0: 
     raise ZeroDivisionError 
    if a == 0: 
     return 0 
    if b < 0: 
     a = negative(a) 
     b = negative(b) 
    A = 0 
    if a < 0: 
     while a < 0: 
      a = add(a, b) 
      A = subtract(A, 1) 
    else: 
     while b < a : 
      a = subtract(a, b) 
      A = add(A, 1) 
    return A 

def mod(a, b): 
    return subtract(a, multiply(div(a, b),b)) 

通過two's compliment否定用來得到一個函數,其中a和b都大於0,並選擇正確的操作(加法或減法)。進一步用減法,注意不要產生負面結果。這是因爲遞歸定義的加減仇恨交叉0.同樣在這種情況下,遞歸加法和減法完成與你的循環完全相同的事情。

+1

它幫助我!多謝! –

+0

@ alex.l我真的搞砸了'negative()'函數總是返回正值...我現在修復了它,並添加了一些更有趣的函數 – Aaron

相關問題