python 二进制数相加
def add_binary_nums(x,y):
max_len = max(len(x), len(y)) x = x.zfill(max_len)
y = y.zfill(max_len) result = ''
carry = for i in range(max_len-, -, -):
r = carry
r += if x[i] == '' else
r += if y[i] == '' else
result = ('' if r % == else '') + result
carry = if r < else if carry != : result = '' + result return result.zfill(max_len) print(add_binary_nums('', ''))
print(add_binary_nums('', ''))
print(add_binary_nums('', ''))
print(add_binary_nums('', ''))
python 二进制数相加的更多相关文章
- [LeetCode] Add Binary 二进制数相加
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...
- LeetCode Add Binary 两个二进制数相加
class Solution { public: string addBinary(string a, string b) { if(a==""&&b==" ...
- Leetcode5078. 负二进制数相加
问题: 5078. 负二进制数相加 给出基数为 -2 的两个数 arr1 和 arr2,返回两数相加的结果. 数字以 数组形式 给出:数组由若干 0 和 1 组成,按最高有效位到最低有效位的顺序排列. ...
- [LeetCode] 67. Add Binary 二进制数相加
Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...
- Python 字符串相加问题
今天在用Python拼接字符串时碰到个问题,从数据库取出来的数据以及获取到的时间等数据拼成了一个字符串a,将字符串a与自定义的字符串b拼接时一直中断,无法继续执行,也没有报错,将数据库取出数据变成自定 ...
- python 字符串相加
我们通过操作符号+来进行字符串的相加,不过建议还是用其他的方式来进行字符串的拼接,这样效率高点. 原因:在循环连接字符串的时候,他每次连接一次,就要重新开辟空间,然后把字符串连接起来,再放入新的空间, ...
- Python字符串相加以及字符串格式化
1.在Python中字符串a占用一块内存地址,字符串b也占用一块内存地址,当字符串a+b时,又会在内存空间中开辟一块新的地址用来存放a+b. a 地址一 b 地址二 a+b 地址三 因此内存中就占了三 ...
- python矩阵相加
举个栗子: # 两个 3 行 3 列的矩阵,实现其对应位置的数据相加,并返回一个新矩阵: # 使用 for 迭代并取出 X 和 Y 矩阵中对应位置的值,相加后放到新矩阵的对应位置中. import n ...
- [Swift]LeetCode1073. 负二进制数相加 | Adding Two Negabinary Numbers
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
随机推荐
- PAT 1029 Median[求中位数][难]
1029 Median(25 分) Given an increasing sequence S of N integers, the median is the number at the midd ...
- rsync 常用命令
rsync -auvrtzopgP --progress --delete --exclude-from=exclude.list SRC DST \\保留原文件属性并详细输出 删除那些DST中SRC ...
- rsync 配置详解
安装 [root@localhost ~]# yum install -y rsync [root@localhost ~]# systemctl start rsyncd [root@localho ...
- [LeetCode] questions conclusion_ Dynamic Programming
Questions: [LeetCode] 198. House Robber _Easy tag: Dynamic Programming [LeetCode] 221. Maximal Squar ...
- 3.keras实现-->高级的深度学习最佳实践
一.不用Sequential模型的解决方案:keras函数式API 1.多输入模型 简单的问答模型 输入:问题 + 文本片段 输出:回答(一个词) from keras.models import M ...
- linux常用命令:sort 命令
sort 命令是Linux系统下一种对文件排序的工具,sort 命令功能十分强大,是 Shell 脚本编程是常用的文件排序工具. 1.命令格式: sort [选项]... [文件]... 或:sort ...
- [转] Oracle学习之创建数据库(新建实例)
由于项目需求,在本机中开发,需要新建oracle数据库实例,亲测可以. 出处:http://blog.csdn.NET/luiseradl/article/details/6972217 http:/ ...
- JSmpeg-用JavaScript编写的视频播放器
使用说明:https://github.com/phoboslab/jsmpeg ffmpeg -i rtmp://abc/ccc/111 -f mpegts -codec:v mpeg1video ...
- 认识epoll
linux下的epoll(7)函数,其有着良好的就绪事件通知机制.Epoll 是被linux2.6开始引进的,但是不被其他的类UNIX系统支持,它提供了一种类似select或poll函数的机制:a. ...
- Win7远程桌面出现凭据不正确的解决办法
在自已平时的开发环境中,出现过WIN7远程桌面凭据不正确,但登录账号和密码是确认正确的问题.解决办法如下图所示: