原题链接

思路:

用一个数保存进制,从后往前不断pop出两个数字和进制数相加,放入返回值中。

var addBinary = function(a, b) {
var arrA = a.split('');
var arrB = b.split(''); var len = Math.max(a.length, b.length), c = 0, result = '';
while(len-- > 0 || c > 0) {
let va = arrA.pop();
let vb = arrB.pop();
if(va) c += parseInt(va);
if(vb) c += parseInt(vb);
result = (c % 2) + result;
c = c > 1 ? 1 : 0;
}
return result;
};

[leetcode] 67. Add Binary (easy)的更多相关文章

  1. LeetCode 67. Add Binary【个位补0,不必对齐】【easy】

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

  2. [leetcode]67. Add Binary 二进制加法

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

  3. LeetCode 67. Add Binary (二进制相加)

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

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

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

  5. LeetCode 67. Add Binary

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

  6. Java [Leetcode 67]Add Binary

    题目描述: Given two binary strings, return their sum (also a binary string). For example,a = "11&qu ...

  7. (String) leetcode 67. Add Binary

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

  8. LeetCode - 67. Add Binary(4ms)

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

  9. leetcode 67. Add Binary (高精度加法)

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

随机推荐

  1. Qt4学习笔记 (7) 本篇说一下Qt对于脚本的支持, 即QtScript模块.

    本篇说一下Qt对于脚本的支持, 即QtScript模块. Qt支持的脚本基于ECMAScript脚本语言, 这个东西又是javascript, jscript的基础. 所以, 一般只要学过javasc ...

  2. 做个知识回顾目录,打算每日更新一下ios的基础知识

    一.基础技能列表:   01 面向对象特性       类与方法封装       通过继承扩展类       抽象类与方法覆盖       多态.动态类型和动态绑定       分类和协议       ...

  3. Codility----PassingCars

    Task description A non-empty zero-indexed array A consisting of N integers is given. The consecutive ...

  4. python机器学习系列之环境搭建

    Windows系统下python2.7,numpy,matplotlib安装 1.  python2.7从https://www.python.org/downloads/release/python ...

  5. Linux使用daemontools

    功能: 在使用memcached时候,怕因为一些不可预知的因素导致memcached进程死掉,而又不能及时的发现重启,可以通过daemontools来管理memcached的启动,当memcached ...

  6. 数据管理工具Flux、Redux、Vuex的区别

    目录 为什么要进行数据管理? 怎么有效地进行数据管理? 数据管理工具 1. Flux 2. Redux 3. Vuex 使用数据管理工具的场景 相关资料 主要讲解一下前端为什么需要进行数据管理,有效的 ...

  7. linux上java和golang环境变量的设置

    JAVA环境变量   (1).打开~/.bashrc完成环境配置( 作用类似于/etc/bashrc, 只是针对用户自己而言,不对其他用户生效.)       文件追加            expo ...

  8. Python文件中将print的输出内容重定向到变量中

    有时候需要用到别人的代码, 但是又不想修改别人的文件, 想拿到输出的结果, 这时候就需要使用sys模块, 将print输出的内容重定向到变量中. Python调用sys模块中的sys.stdout, ...

  9. leadcode的Hot100系列--206. 反转链表

    这里使用两种方式, 一个是直接从头往后遍历 -------> 迭代 一个是从最后一个往前遍历 -----> 递归 迭代 定义三个变量:pPre pNext pNow pPre表示当前节点的 ...

  10. 5分钟快速部署ownCloud私有云盘存储系统

    ownCloud 是一个开源免费专业的私有云存储项目,它能帮你快速在个人电脑或服务器上架设一套专属的私有云文件同步网盘,可以像 Dropbox 那样实现文件跨平台同步.共享.版本控制.团队协作等等.o ...