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的下标低位再最左边。

(2)我的处理方式是先把两个string 翻转,逆序存储结果,再把结果翻转。有点麻烦,但容易实现

(3)也可以不翻转,从字符串后面开始遍历

code:

class Solution {
public:
    string addBinary(string a, string b) {
        string::size_type len1=a.size();
        string::size_type len2=b.size();
        if(len1==0)
            return b;
        if(len2==0)
            return a;
        unsigned int count=min(len1,len2);
        reverse(a.begin(),a.end());
        reverse(b.begin(),b.end());
        int flag=0;
        string ret;
        for(int i=0;i<count;i++)
        {
            int tmp= a.at(i)-'0'+b.at(i)-'0'+flag;
            cout<<"tmp: "<<tmp<<endl;
            flag=tmp/2;
            ret.push_back(tmp%2+'0');
        }
        if(len1==len2 && flag>0)   //进位
                    ret.push_back('1');
        if(len1>count)//a剩余
        {
            for(int j=count;j<len1;j++)
            {
                int tmp1=a.at(j)-'0'+flag;
                flag=tmp1/2;
                ret.push_back(tmp1%2+'0');
            }
            if(flag>0)
                ret.push_back('1');
        }
        if(len2>count)//b剩余
        {
            for(int k=count;k<len2;k++)
            {
                int tmp2=b.at(k)-'0'+flag;
                flag=tmp2/2;
                ret.push_back(tmp2%2+'0');
            }
            if(flag>0)
                ret.push_back('1');
        }
        reverse(ret.begin(),ret.end());
        return ret;
    }
};
时间: 2024-08-29 13:26:28

leetcode || 67、Add Binary的相关文章

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

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

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——3、Add Digits

唉,做完这道题目后,有种羞愧无比的感觉,果然还是读书太少... 什么意思呢?就像这道题目一样,要求你编程计算出从1加到100的结果.于是你用循环从1累加到100,结果为5050.而大家都知道,从数学上的角度来解答,结果就是(a[1]+a[n])* n / 2. LeetCode上面的这道[Add Digits]题,其实是道数学题.它这个求解的结果,在数学上称为数根. 原题大概意思是这样,给定一个正整数,循环累加它的各位,直到结果为一位数.如 num 是38,计算的过程是 3+8=11,1+1=2

leetcode || 99、Recover Binary Search Tree

problem: Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Note: A solution using O(n) space is pretty straight forward. Could you devise a constant space solution? confused what "{1,#

LeetCode OJ:Add Binary(二进制相加)

Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 简单的二进制相加而已,只不过传入的参数是字符串而已.为了方便,先将string  reverse了一下,代码如下: 1 class Solution { 2 public: 3 string addBinary(string a, s

leetcode || 96、Unique Binary Search Trees

problem: Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For example, Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 Hide Tags Tree Dynamic Programming 题意:大小为

leetcode || 106、Construct Binary Tree from Inorder and Postorder Traversal

problem: Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. Hide Tags Tree Array Depth-first Search 题意:给定二叉树的中序遍历序列和后续遍历序列,构建这棵二叉树,也很经典 thinking: (1)这种类型的题,要举个例子找

leetcode || 98、Validate Binary Search Tree

problem: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes