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

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

解题思路:

从两个子字符尾部遍历字符;

每次得到新的字符插入结果字符串的头部;

解题步骤:

1、建立返回string、从后向前遍历的idx:idx_a / idx_b、进位量
2、循环开始,当idx_a或者idx_b任意不为0时,循环继续:
 (1)临时整形sum = 进位值;
 (2)如果idx_a不为0,则加上a[idx_a - 1],idx_a--;同理对idx_b;
 (3)更新进位值和sum,并将sum以字符的形式插入返回string的头部;
3、如果循环结束时进位值不为0,则在返回string头部添加一位。

代码:

 class Solution {
public:
string addBinary(string a, string b) {
int len_a = a.size();
int len_b = b.size();
int sig_flag = ;
string ret; while (len_a || len_b) {
int curd = sig_flag;
if (len_a) {
curd += a[len_a - ] - '';
len_a--;
} if (len_b) {
curd += b[len_b - ] - '';
len_b--;
} sig_flag = curd / ;
curd = curd % ;
ret.insert(, , '' + curd);
} if (sig_flag)
ret.insert(, , ''); return ret;
}
};

附录:

string操作

int char string

【Leetcode】【Easy】Add Binary的更多相关文章

  1. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  2. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  3. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  4. ACM金牌选手整理的【LeetCode刷题顺序】

    算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...

  5. 【LeetCode】66 & 67- Plus One & Add Binary

    66 - Plus One Given a non-negative number represented as an array of digits, plus one to the number. ...

  6. 【LeetCode每天一题】Add Binary(二进制加法)

    Given two binary strings, return their sum (also a binary string).The input strings are both non-emp ...

  7. 【leetcode刷题笔记】Add Binary

    Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...

  8. 【LeetCode算法题库】Day1:TwoSums & Add Two Numbers & Longest Substring Without Repeating Characters

    [Q1] Given an array of integers, return indices of the two numbers such that they add up to a specif ...

  9. 【leetcode刷题笔记】Unique Binary Search Trees II

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

  10. 【leetcode刷题笔记】Binary Tree Level Order Traversal(JAVA)

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

随机推荐

  1. 把磁力下载站改为python系统

    已经一年半载没有写博客了,搞得上来不知道写些什么. 索马里影视下载  WWW.IBMID.COM  现在用的是CENTOS 7 系统, 经历了多次点技术变更.开源版本使用了django网站框架重写,之 ...

  2. POJ 2763 (LCA +RMQ+树状数组 || 树链部分) 查询两点距离+修改边权

    题意: 知道了一颗有  n 个节点的树和树上每条边的权值,对应两种操作: 0 x        输出 当前节点到 x节点的最短距离,并移动到 x 节点位置 1 x val   把第 x 条边的权值改为 ...

  3. [转] 利用js实现 禁用浏览器后退

    [From] http://blog.csdn.net/zc474235918/article/details/53138553 现在很多的内部系统,一些界面,都是用户手动点击退出按钮的.但是为了避免 ...

  4. async中series的实现 javascript构件

    //同步流程 var series=function(arr){ function async(i){ arr[i](function(){ if(1+i<arr.length){ async( ...

  5. 转: centos系统home下的中文目录改为英文目录

    转自h t t p : / /xugang-1017-126-com.iteye.com/blog/2081845 如果安装了中文版的Cent OS之后,root目录和home目录下会出现中文的路径名 ...

  6. element-ui Form表单验证

    element-ui Form表单验证规则全解 element的form表单非常好用,自带了验证规则,用起来很方便,官网给的案例对于一些普通场景完全没问题,不过一些复杂场景的验证还得自己多看文档摸索, ...

  7. rewrite 功能

    一, rewrite 地址重写与地址转发 区别: 1,地址转发后客户端浏览器地址栏中的地址时不会改变的;而地址重写的话客户端浏览器地址栏会改变为服务器确定的地址 2, 在一次地址转发过程中,只产生一次 ...

  8. (转) 来自: http://man.linuxde.net/tee

    tee命令文件过滤分割与合并 tee命令用于将数据重定向到文件,另一方面还可以提供一份重定向数据的副本作为后续命令的stdin.简单的说就是把数据重定向到给定文件和屏幕上. 存在缓存机制,每1024个 ...

  9. 【转】python平台libsvm安装

    来源:http://blog.csdn.net/prom1201/article/details/51382358 网上有很多麻烦的在win64机器上安装libsvm的步骤,实际上只要在下面网站找到l ...

  10. Oracle命令整理

    1 常用命令 常用命令 1 sqlplus  scott/tiger@192.168.47.10:1521/orcl      后面不要加: sqlplus  sys/oracle  as sysdb ...