【leetcode?python】Sum Of Two Number

#-*- coding: UTF-8 -*-
#既然不能使用加法和减法,那么就用位操作。下面以计算5+4的例子说明如何用位操作实现加法:
#1. 用二进制表示两个加数,a=5=0101,b=4=0100;
#2. 用and(&)操作得到所有位上的进位carry=0100;
#3. 用xor(^)操作找到a和b不同的位,赋值给a,a=0001;
#4. 将进位carry左移一位,赋值给b,b=1000;
#5. 循环直到进位carry为0,此时得到a=1001,即最后的sum。

#!!!!!!关于负数的运算。python的位运算!!!!
#上面思路还算正常,然而对于Python就有点麻烦了。因为Python的整数不是固定的32位,
#所以需要做一些特殊的处理,具体见代码吧。
#代码里的将一个数对0x100000000取模(注意:Python的取模运算结果恒为非负数),是希望该数的二进制表示从第32位开始到更高的位都同是0(最低位是第0位)
#以在0-31位上模拟一个32位的int。
#没有懂: (~0x100000000+1) 表示 【0x100000000=4294967296】的负数:-4294967296
class Solution(object):
    def getSum(self, a, b):

while b:
            carry=a&b
            a=(a^b) % 0x100000000
            b=(carry<<1)% 0x100000000

return a if a <= 0x7FFFFFFF else a | (~0x100000000+1)

sol=Solution()
print sol.getSum(-12,-2)

时间: 2024-08-10 19:18:30

【leetcode?python】Sum Of Two Number的相关文章

【leetcode?python】 Sum of Left Leaves

#-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):#     def __init__(self, x):#         self.val = x#         self.left = None#         self.right = None class Solution(object):        def dfs(self,root,isLeft):     

【LeetCode OJ】Sum Root to Leaf Numbers

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 # Definition for a  binary tree node # class TreeNode: #     def __init__(self, x): #         self.val = x #         self.left = No

【leetcode?python】Convert a Number to Hexadecimal

#-*- coding: UTF-8 -*- class Solution(object):    hexDic={0:'0',1:'1',2:'2',3:'3',4:'4',5:'5',6:'6',7:'7',8:'8',9:'9',\            10:'a',            11:'b',            12:'c',            13:'d',            14:'e',            15:'f'}            def t

【leetcode?python】Binary Watch

#-*- coding: UTF-8 -*- from itertools import combinations class Solution(object):        hourList=[8,4,2,1]    minList=[32,16,8,4,2,1]            def selectHour(self,hourNum):        if hourNum==0:            return [0]        #迭代工具模块包含了操做指定的函数用于操作迭代

【leetcode?python】 19. Remove Nth Node From End of List

双指针思想:两个指针相隔n-1,每次两个指针向后一步,当后面一个指针没有后继了,前面一个指针的后继就是要删除的节点 # Definition for singly-linked list.# class ListNode(object):#     def __init__(self, x):#         self.val = x#         self.next = None class Solution(object):    def removeNthFromEnd(self,

【leetcode?python】 36. Valid Sudoku

数独规则如下:相当于一个9*9的矩阵 代码如下:#特定的九个格内1-9的个数至多为1#依次检查每行,每列,每个子九宫格是否出现重复元素,如果出现返回false,否则返回true.class Solution(object):    def isValidSudoku(self, board):        """        :type board: List[List[str]]        :rtype: bool        """

【leetcode?python】 111. Minimum Depth of Binary Tree

# Definition for a binary tree node.# class TreeNode(object):#     def __init__(self, x):#         self.val = x#         self.left = None#         self.right = None class Solution(object):    depthList=[]    def minDepth(self, root):        ""&q

【leetcode?python】 203. Remove Linked List Elements

# Definition for singly-linked list.# class ListNode(object):#     def __init__(self, x):#         self.val = x#         self.next = None class Solution(object):    def removeElements(self, head, val):        """        :type head: ListNode

【leetcode?python】 88. Merge Sorted Array

class Solution(object):    def merge(self, nums1, m, nums2, n):        """        :type nums1: List[int]        :type m: int        :type nums2: List[int]        :type n: int        :rtype: void Do not return anything, modify nums1 in-place