LeetCode – Refresh – Add Binary

This question is pretty straight forward.

 1 class Solution {
 2 public:
 3     string addBinary(string a, string b) {
 4         int runnerA = a.size()-1, runnerB = b.size()-1, carry = 0;
 5         if (runnerA < 0) return b;
 6         if (runnerB < 0) return a;
 7         string result;
 8         while (runnerA >= 0 || runnerB >= 0) {
 9             int sum = carry;
10             if (runnerA >= 0) sum += int(a[runnerA--] - ‘0‘);
11             if (runnerB >= 0) sum += int(b[runnerB--] - ‘0‘);
12             carry = sum/2;
13             result = char(sum%2 + ‘0‘) + result;
14         }
15         if (carry) result = ‘1‘ + result;
16         return result;
17     }
18 };
时间: 2024-10-05 09:59:20

LeetCode – Refresh – 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][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 *

LeetCode – Refresh – Add Two Numbers

Same with add binary. You can also skip delete the result pointer. But just return result->next. Depends on the interviewer. 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val

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 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

LeetCode 67. Add Binary

Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 这个题目只要注意各种情况你就成功了一大半,特别要注意的是对进位赋值后可能产生的变化,以及最后一位进位为1时, 我们要把这个1插进来.同时注意字符串的低位是我们数值的高位 class Solution { public: string

LeetCode[String]: Add Binary

Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". 我的C++实现代码如下: string addBinary(string a, string b) { string sum; int carry = 0; for (int i = a.size() - 1, j = b.si

[leetcode] 16. Add Binary

这个题目相对有点奇怪,题目如下: Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". 可能是我思路的问题吧,我之前是在考虑先将string转成int或者double,然后将二进制转为十进制进行计算,最后再将这个十进制转为二进制再转为string返回.这个思路本来是没问题的.但是.