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

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

算法:模拟加法过程

 class Solution {
public:
string addBinary(string a, string b) {
int len1=a.size();
int len2=b.size();
int c=;
reverse(a.begin(),a.end());
reverse(b.begin(),b.end());
string s="";
int len=max(len1,len2);
for(int i=;i<len;i++)
{
int t=;
if(i<len1) t+=a[i]-'';
if(i<len2) t+=b[i]-'';
t+=c;
c=t/;
s=(char)(t%+'')+s;
}
if(c>) s=(char)(c+'')+s;
return s;
}
};

Add Binary <leetcode>的更多相关文章

  1. leetcode解题:Add binary问题

    顺便把之前做过的一个简单难度的题也贴上来吧 67. Add Binary Given two binary strings, return their sum (also a binary strin ...

  2. [LintCode] Add Binary 二进制数相加

    Given two binary strings, return their sum (also a binary string). Have you met this question in a r ...

  3. Add Binary

    Add Binary https://leetcode.com/problems/add-binary/ Given two binary strings, return their sum (als ...

  4. LeetCode 面试:Add Binary

    1 题目 Given two binary strings, return their sum (also a binary string). For example,a = "11&quo ...

  5. 67. Add Binary【LeetCode】

    67. Add Binary Given two binary strings, return their sum (also a binary string). For example,a = &q ...

  6. [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings

    这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...

  7. 2016.6.21——Add Binary

    Add Binary 本题收获: 对于相加而言,考虑进位以及进位之后的值为多少,全部进位完毕后进位还为1(11 + 11 = 110)需要添加一位.1.string中默认每个元素为char型 2.从i ...

  8. LeetCode: Add Binary 解题报告

    Add BinaryGiven two binary strings, return their sum (also a binary string). For example,a = "1 ...

  9. leetcode笔记:Add Binary

    一.题目描写叙述 Given two binary strings, return their sum (also a binary string). For example, a = "1 ...

  10. # Leetcode 67:Add Binary(二进制求和)

    Leetcode 67:Add Binary(二进制求和) (python.java) Given two binary strings, return their sum (also a binar ...

随机推荐

  1. 纯js轮播效果(减速效果)待改进

    HTML代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. ...

  2. LintCode Binary Tree Inorder Traversal

    Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values ...

  3. Flex Builder读书笔记(一)

    Flex表现层服务器包含Flex应用框架和Flex运行服务,Flex框架包含MXML描述性语言.ActionScript程序语言和Flex类库.MXML原理定义用户界面元素,ActionScript定 ...

  4. Jqgrid 数据格式化配置

    默认格式化 $jgrid = { formatter : { integer : {thousandsSeparator: " ", defaultValue: '0'}, num ...

  5. LA 4329 ping-pong树状数组

    题目链接: 刘汝佳,大白书,P197. 枚举裁判的位置,当裁判为i时,可以有多少种选法,如果已经知道在位置i之前有ci个数比ai小,那么在位置i之前就有i-1-ci个数比ai大. 在位置i之后有di个 ...

  6. 论文笔记之:A CNN Cascade for Landmark Guided Semantic Part Segmentation

    A CNN Cascade for Landmark Guided Semantic Part Segmentation  ECCV 2016 摘要:本文提出了一种 CNN cascade (CNN ...

  7. express创建项目

    sudo apt-get install node-express-generator dave@voctrals:~/WebstormProjects/nodejs-study/express$ e ...

  8. .NET微信通过授权获取用户的基本信息

    一.填写授权回调页面的域名 二.引导用户到指定的授权页面 例如:https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID& ...

  9. linux下socket编程-进程间通信

    一.什么是Socket Socket接口是TCP/IP网络通信的API,Socket接口定义了许多函数或例程,可以用它们来开发TCP/IP网络上的应用程序. Socket类型有两种:流式Socket ...

  10. sql 下,float和numeric

    搜了下两者的区别,答案其实很明显: numeric支持最长38位长度的数字,只有在你要的数字的长度超过38位时才推荐使用float 原因是,float在保存和提取,以及数学计算等方面,会产生于期待记过 ...