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. springboot(三)-使用JSP

    Springboot的默认视图支持是Thymeleaf.这里先不谈,这么优秀的框架怎么可能不能使用JSP呢?不允许的. 那么需要添加对jsp的支持. pom.xml 在pom.xml文件中添加依赖 & ...

  2. swiper、fullPage、hammer几种滑动插件对比

    1.使用hammer,自己实现滑动垂直切换页面 <!DOCTYPE html> <html lang="en"> <head> <titl ...

  3. js实现点击按钮滚动条缓慢滚动到顶部

    toTop:function(){ //toTop 滚动到顶部 var currentPosition,timer; var speed=10; timer=setInterval(function( ...

  4. C++ GUI Qt4编程(05)-2.2GoToCell

    1. 使用Qt设计师创建GoToCell对话框. 2. gotocelldialog.cpp #include <QRegExp> #include "gotocelldialo ...

  5. my31_MGR单写模式压测以及对比普通从库记录

    场景MGR单写模式三节点,db46写节点,db47/db48为读节点工具sysbencn.压测15个小时,db46上18线程纯写,12线程oltp混合测试,db48上12线程select在压测2个小时 ...

  6. $bzoj1052-HAOI2007$ 覆盖问题 抽屉原理 二分答案

    题面描述 某人在山上种了\(N\leq 2*10^4​\)棵小树苗.冬天来了,温度急速下降,小树苗脆弱得不堪一击,于是树主人想用一些塑料薄膜把这些小树遮盖起来,经过一番长久的思考,他决定用\(3​\) ...

  7. (转)Cobbler无人值守批量安装Linux系统

    本文目录: 1.1 pxe安装系统 1.2 cobbler基本介绍 1.3 安装和配置cobbler 1.3.1 安装cobbler 1.3.2 配置dhcp和tftp 1.4 cobbler从本地光 ...

  8. Hadoop Ecosystem related ports

    本文总结了Hadoop生态系统中各个组件使用的端口,包括了HDFS,Map Reduce,HBase,Hive,Spark,WebHCat,Impala,Alluxio,Sqoop等,后续会持续更新. ...

  9. 新手 php连接数据库大概。简单过程浅析以及遇到的问题分析

    原文作者:aircraft 原文地址: https://www.cnblogs.com/DOMLX/p/8116845.html 重点:PHP运行在服务器上的请记住!!! 1.在连接数据库与PHP之前 ...

  10. React.js 小书 Lesson15 - 实战分析:评论功能(二)

    作者:胡子大哈 原文链接:http://huziketang.com/books/react/lesson15 转载请注明出处,保留原文链接和作者信息. 上一节我们构建了基本的代码框架,现在开始完善其 ...