[leetcode] 67. Add Binary (easy)
思路:
用一个数保存进制,从后往前不断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)的更多相关文章
- LeetCode 67. Add Binary【个位补0,不必对齐】【easy】
Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...
- [leetcode]67. Add Binary 二进制加法
Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...
- LeetCode 67. Add Binary (二进制相加)
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...
- [LeetCode] 67. Add Binary 二进制数相加
Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...
- LeetCode 67. Add Binary
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...
- Java [Leetcode 67]Add Binary
题目描述: Given two binary strings, return their sum (also a binary string). For example,a = "11&qu ...
- (String) leetcode 67. Add Binary
Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...
- LeetCode - 67. Add Binary(4ms)
Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...
- leetcode 67. Add Binary (高精度加法)
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...
随机推荐
- 梅林路由器 开启ssh key远程登录
转载自 开启SSH KEY在手机远程登陆路由 http://koolshare.cn/thread-67565-1-1.html (出处: KoolShare) 首先修改路由的登录名和密码 下载put ...
- JavaWeb实现上传文件
需要 commons-io与commons-fileupload 首先在jsp中创建一下布局 <%@ page contentType="text/html;charset=UTF-8 ...
- QThread多线程编程经典案例分析(三种方法,解释了为什么使用moveToThread的根本原因,即为了避免调用QThread::exec() )
传统的图形界面应用程序都只有一个线程执行,并且一次执行一个操作.如果用户调用一个比较耗时的操作,就会冻结界面响应. 一个解决方法是按照事件处理的思路: 调用 Void QApplication::pr ...
- 打开并锁定一个文件(使用LockFile API函数)
var aHandle : THandle; aFileSize : Integer; aFileName : String; procedure TForm1.Button3Click(Sender ...
- EF Power Tool 代码生成器 反向生成
大致来说,这个工具有这样几个功能: 1) 按照现有数据库结构,生成Code First POCO class.DbContext class和相应的mapping class. 2) 以designe ...
- Django预备知识
http协议 url: 协议://域名(IP)+端口(80)/路径?参数(a=1&b=2) 示例:https://www.baidu.com/s/?wd=aaa MVC M:mdoel 与数据 ...
- spring boot单元测试之RestTemplate(一)
写代码重要,写好的代码准确无误,且符合预期那更是必不可少. spring boot内嵌了专门的单元测试模块——RestTemplate,保证了程序员可以对自己的代码进行及时的测试. 闲言少叙,直接上代 ...
- RequestMapping原理分析和RequestMappingHandlerMapping
转载https://juejin.im/post/5cbeadb96fb9a031ff0d18b5 源码版本spring-webmvc-4.3.7.RELEASE 使用Spring MVC的同学一般都 ...
- Java多线程(三):Synchronized
多线程安全 脏读:多个线程对同一个对象的实例变量进行修改后访问,导致读到的数据是被修改过的. 实例 ThreadDomain16类 public class ThreadDomain16 { priv ...
- visudo 与 /etc/sudoers
增加多个用户免密码登录 User_Alias USER_OPS = zouyi,hanerhui,shibeibei,gaoxudong,xiaoyuelin,wangsongfeng,sunjian ...