【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)
【leetcode❤python】Sum Of Two Number的更多相关文章
- 【leetcode❤python】 414. Third Maximum Number
#-*- coding: UTF-8 -*- #l1 = ['1','3','2','3','2','1','1']#l2 = sorted(sorted(set(l1),key=l1.index,r ...
- 【leetcode❤python】 Sum of Left Leaves
#-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):# def __init ...
- 【leetcode❤python】 1. Two Sum
#-*- coding: UTF-8 -*- #AC源码[意外惊喜,还以为会超时]class Solution(object): def twoSum(self, nums, target): ...
- 【leetcode❤python】 303. Range Sum Query - Immutable
#-*- coding: UTF-8 -*- #Tags:dynamic programming,sumRange(i,j)=sum(j)-sum(i-1)class NumArray(object) ...
- 【leetcode❤python】 112. Path Sum
#-*- coding: UTF-8 -*-# Definition for a binary tree node.# class TreeNode(object):# def __init_ ...
- 【leetcode❤python】 374. Guess Number Higher or Lower
#-*- coding: UTF-8 -*-# The guess API is already defined for you.# @param num, your guess# @return - ...
- 【leetcode❤python】 9. Palindrome Number
#回文数#Method1:将整数转置和原数比较,一样就是回文数:负数不是回文数#这里反转整数时不需要考虑溢出,但不代表如果是C/C++等语言也不需要考虑class Solution(object): ...
- 【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', ...
- 【leetcode❤python】263. Ugly Number
class Solution(object): def isUgly(self, num): if num<=0:return False comlist=[2 ...
随机推荐
- angular 自定义指令 link
function link(scope, element, attrs) { ... } where: scope is an Angular scope object. element is the ...
- [经典php视频]构建正则表达式解析网页中的图像标记<img>
这是高洛峰php视频中的一段,视频中一边分析需要的功能,一边构建greg_match函数的参数,边讲解边实战,是非常好的一种构建功能的演示. 你不可能把浩瀚的IT资料都记在脑袋里,也不可能随时随地透过 ...
- wc之上传图片
wc上传图片:以上传头像为例 1.找到views/default/account/settings/profile.tpl.htm中 init_avatar_uploader($('#avatar_u ...
- 我的代码观——关于ACM编程风格与librazy网友的对话
序 在拙文 <高手看了,感觉惨不忍睹——关于“[ACM]杭电ACM题一直WA求高手看看代码”>中,我对ACMer们的一些代码“惯例”发表了我的看法, librazy网友在评论中给出了他的一 ...
- Android多线程通信之Handler
主线程 public class MainActivity extends ActionBarActivity { private Handler handler; // private Thread ...
- mysql开启远程访问
1.MySql-Server 出于安全方面考虑只允许本机(localhost, 127.0.0.1)来连接访问. 这对于 Web-Server 与 MySql-Server 都在同一台服务器上的网站架 ...
- Hadoop3.0新特性介绍,比Spark快10倍的Hadoop3.0新特性
Hadoop3.0新特性介绍,比Spark快10倍的Hadoop3.0新特性 Apache hadoop 项目组最新消息,hadoop3.x以后将会调整方案架构,将Mapreduce 基于内存+io+ ...
- ubunu下用命令设置壁纸
ubunu下用命令设置壁纸: gsettings set org.gnome.desktop.background picture-uri “file:[fileName]” eg:gsettings ...
- Valid Anagram
class Solution { public: bool isAnagram(string s, string t) { if(t=="") return s=="&q ...
- MySQL存储引擎之InnoDB
一.The InnoDB Engine Each InnoDB table is represented on disk by an .frm format file in the database ...