LeetCode 第 67 题 (Add Binary)

LeetCode 第 67 题 (Add Binary)

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

For example,

a = “11”

b = “1”

Return “100”.

两个字符串,计算加法。这道题主要是考察对字符串操作的掌握情况。另外,加法要从低位算起,但是输出时要先输出高位。因此,需要将计算结果先存下来,然后再逆序输出。

访问字符串的字符可以采用传统的 c 语言那种数组下标的方式。也可以用 iterator。

下标方式的代码如下:

string addBinary(string a, string b)
{
    int n1 = a.length() - 1;
    int n2 = b.length() - 1;

    char ai, bi, ci, carry = 0;
    stack<char> out;
    while (n1 >= 0 || n2 >= 0)
    {
        ai = (n1 >= 0) ? a[n1--] - ‘0‘ : 0;
        bi = (n2 >= 0) ? b[n2--] - ‘0‘ : 0;

        ci = ai + bi + carry;
        carry = (ci > 1) ? 1 : 0;
        ci = ci & 0x01;
        out.push(ci);
    }
    if(carry > 0) out.push(carry);

    string c;
    while(!out.empty())
    {
        ci = out.top() + ‘0‘;
        c.push_back(ci);
        out.pop();
    }
    return c;
}

iterator 方式有点特殊,对字符串逆序遍历需要用 reverse_iterator。代码如下:

string addBinary(string a, string b)
{
    string::const_reverse_iterator ita = a.crbegin();
    string::const_reverse_iterator itb = b.crbegin();

    char ai, bi, ci, carry = 0;
    stack<char> out;
    while (ita != a.crend() || itb != b.crend())
    {
        ai = (ita != a.crend()) ? *ita++ - ‘0‘ : 0;
        bi = (itb != b.crend()) ? *itb++ - ‘0‘ : 0;

        ci = ai + bi + carry;
        carry = (ci > 1) ? 1 : 0;
        ci = ci & 0x01;
        out.push(ci);
    }
    if(carry > 0) out.push(carry);

    string c;
    while(!out.empty())
    {
        ci = out.top() + ‘0‘;
        c.push_back(ci);
        out.pop();
    }
    return c;
}
时间: 2024-08-29 21:58:10

LeetCode 第 67 题 (Add Binary)的相关文章

LeetCode【67】Add Binary

Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 看起来挺好写的样子,没想到墨迹了半天. string add(string a, string b,int lena,int lenb) { if(lena<lenb) return add(b,a,lenb,lena); //

leetcode学习笔记:Add Binary

一.题目描述 Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". 二.解题技巧 这道题考察两个二进制数相加,考虑到输入的是两组string,同时注意在运算时从左到右分别是从低位到高位,因此需要考虑对输入进行翻转处理,中间二进制树相加部分没有过多的设计障碍,主要是计算进位:在两组数

leetcode_67题——Add Binary(字符串string,反向迭代器reverse_iterator,栈stack)

Add Binary Total Accepted: 39288 Total Submissions: 158078My Submissions Question Solution Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". Hide Tags Math String Have

leetcode || 67、Add Binary

problem: Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". Hide Tags Math String 题意:二进制数相加,二进制数用string表示 thinking: (1)这题简单,但是有一个坑很容易掉进去:二进制的数的低位在最右边,string的下标低位再最左边

LeetCode(67)题解: Add Binary

https://leetcode.com/problems/add-binary/ 题目: Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 思路: 数组模拟加法操做,注意首位进一的情况. class Solution { public: string addBinary(strin

第30题 Add Binary

Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". Hide Tags Math String Solution in Java: public class Solution { public String addBinary(String a, String b) { int

【LeetCode每天一题】Binary Tree Zigzag Level Order Traversal(二叉树的之字形遍历)

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example:Given binary tree [3,9,20,null,null,15,7], 3 / 9 20 / 15 7 return it

【LeetCode每天一题】Binary Tree Preorder Traversal(前序遍历)

Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [1, null, 1,2,3 ] 1 2 / 3 Output: [1,2,3] Follow up: Recursive solution is trivial, could you do it iteratively? 思路 二叉树的前序遍历方法分为递归法和使用循环辅助栈的方法,递归方法我们在递归左右节点之前先将当

LeetCode 第67题 不同路径

一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为"Start" ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为"Finish"). 问总共有多少条不同的路径? 例如,上图是一个7 x 3 的网格.有多少可能的路径? 说明:m 和 n 的值均不超过 100. 示例 1: 输入: m = 3, n = 2输出: 3解释:从左上角开始,总共有 3 条路径可以到达右下角.1. 向右 -> 向右 -> 向下