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 二进制数相加的更多相关文章

  1. [LeetCode] Add Binary 二进制数相加

    Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...

  2. LeetCode Add Binary 两个二进制数相加

    class Solution { public: string addBinary(string a, string b) { if(a==""&&b==" ...

  3. Leetcode5078. 负二进制数相加

    问题: 5078. 负二进制数相加 给出基数为 -2 的两个数 arr1 和 arr2,返回两数相加的结果. 数字以 数组形式 给出:数组由若干 0 和 1 组成,按最高有效位到最低有效位的顺序排列. ...

  4. [LeetCode] 67. Add Binary 二进制数相加

    Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...

  5. Python 字符串相加问题

    今天在用Python拼接字符串时碰到个问题,从数据库取出来的数据以及获取到的时间等数据拼成了一个字符串a,将字符串a与自定义的字符串b拼接时一直中断,无法继续执行,也没有报错,将数据库取出数据变成自定 ...

  6. python 字符串相加

    我们通过操作符号+来进行字符串的相加,不过建议还是用其他的方式来进行字符串的拼接,这样效率高点. 原因:在循环连接字符串的时候,他每次连接一次,就要重新开辟空间,然后把字符串连接起来,再放入新的空间, ...

  7. Python字符串相加以及字符串格式化

    1.在Python中字符串a占用一块内存地址,字符串b也占用一块内存地址,当字符串a+b时,又会在内存空间中开辟一块新的地址用来存放a+b. a 地址一 b 地址二 a+b 地址三 因此内存中就占了三 ...

  8. python矩阵相加

    举个栗子: # 两个 3 行 3 列的矩阵,实现其对应位置的数据相加,并返回一个新矩阵: # 使用 for 迭代并取出 X 和 Y 矩阵中对应位置的值,相加后放到新矩阵的对应位置中. import n ...

  9. [Swift]LeetCode1073. 负二进制数相加 | Adding Two Negabinary Numbers

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

随机推荐

  1. hdu3339In Action(最短路+01背包)

    http://acm.sdut.edu.cn:8080/vjudge/contest/view.action?cid=259#problem/H Description Since 1945, whe ...

  2. [Leetcode] 336. Palindrome Pairs_Hard

    Given a list of unique words, find all pairs of distinct indices (i, j) in the given list, so that t ...

  3. [LeetCode] 221. Maximal Square _ Medium Tag: Dynamic Programming

    Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and re ...

  4. 机器学习理论基础学习10--- 高斯混合模型GMM

    一.什么是高斯混合模型? 高斯混合模型(Gaussian Mixed Model)指的是多个高斯分布函数的线性组合,理论上GMM可以拟合出任意类型的分布,通常用于解决同一集合下的数据包含多个不同的分布 ...

  5. Oracle的FIXED_DATE参数

    今天发现一个有意思的问题, 我们知道,在Oracle数据库中正常执行 select sysdate from dual 都可以返回当前主机的系统时间. 正常修改系统时间,对应的查询结果也会变成修改后的 ...

  6. Twitter OA prepare: Flipping a bit

    You are given a binary array with N elements: d[0], d[1], ... d[N - 1]. You can perform AT MOST one ...

  7. zw版【转发·台湾nvp系列Delphi例程】HALCON FastThreshold2

    zw版[转发·台湾nvp系列Delphi例程]HALCON FastThreshold2 FastThreshold_Delphi_2.PNG procedure TForm1.Button1Clic ...

  8. 数组相同的key组成新的数组

    function dataFunc (data){ var b = {}; $.each(data, function (i,v){ var g = v[key], //将评价人相同的拿出来组成一个新 ...

  9. new Runnable中的 this

    package com.test; public class BB extends AA{ public String bb = "bbbbb"; public void gg() ...

  10. SNMP学习笔记之iReasoning MIB Browser

    0x00 MIB Browser iReasoning MIB浏览器是一个强大和易于使用的工具由iReasoning SNMP API提供支持. MIB浏览器是工程师管理启用SNMP的网络设备和应用程 ...