[LeetCode][JavaScript]Add Binary

https://leetcode.com/problems/add-binary/

Add Binary

Given two binary strings, return their sum (also a binary string).

For example,
a = "11"
b = "1"
Return "100".



长整型二进制加法。

 1 /**
 2  * @param {string} a
 3  * @param {string} b
 4  * @return {string}
 5  */
 6 var addBinary = function(a, b) {
 7     if(a.length < b.length){
 8         var tmp = a;
 9         a = b;
10         b = tmp;
11     }
12
13     var res = "";
14     var carry = 0;
15     for(var i = 1; i <= a.length; i++){
16         var _a = parseInt(a[a.length - i]);
17         var _b = parseInt(b[b.length - i]) || 0;
18         var add = _a + _b + carry;
19         if(add === 3){
20             res = "1" + res;
21             carry = 1;
22         }else if(add === 2){
23             res = "0" + res;
24             carry = 1;
25         }else{
26             res = add + res;
27             carry = 0;
28         }
29     }
30
31     if(carry === 1){
32         res = "1" + res;
33     }
34     return res;
35 };

不让我调自带函数一行搞定,精度不够,机智的test case。

/*
var addBinary = function(a, b) {
    return (parseInt(a, 2) + parseInt(b, 2)).toString(2);
};
*/
时间: 2024-12-17 10:18:45

[LeetCode][JavaScript]Add Binary的相关文章

【LeetCode】Add Binary

Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". public class Solution { public String addBinary(String a, String b) { if(a.equalsIgnoreCase("")||a==null) r

Leetcode 数 Add Binary

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Add Binary Total Accepted: 9509 Total Submissions: 37699 Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". 题意:

leetCode 67. Add Binary 字符串

67. Add Binary Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 思路: 1.将两个字符串按数组相加得到新数组. 2.将新数组转换成结果. 代码如下: class Solution { public:     string addBinary(string a, str

[LeetCode][JavaScript]Unique Binary Search Trees II

Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example,Given n = 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1

[LeetCode][JavaScript]Invert Binary Tree

Invert Binary Tree Invert a binary tree. 4 / 2 7 / \ / 1 3 6 9 to 4 / 7 2 / \ / 9 6 3 1 Trivia:This problem was inspired by this original tweet by Max Howell: Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a

[LeetCode][JavaScript]Balanced Binary Tree

Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. https://leetco

[LeetCode][JavaScript]Add and Search Word - Data structure design

Add and Search Word - Data structure design Design a data structure that supports the following two operations: void addWord(word) bool search(word) search(word) can search a literal word or a regular expression string containing only letters a-z or 

[LeetCode][JavaScript]Add Two Numbers

Add Two Numbers You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (

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

Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 题目标签:Math 题目给了我们两个string a 和 b,让我们把这两个二进制 相加. 首先把两个string 的长度得到,然后从右向左 取 两个string 的 digit. 增设一个 carry = 0: 每一轮把 digit