题目:

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

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

思路:

递归,注意递归终止条件为i < 0 && j < 0 && carray < 1

package manipulation;public class AddBinary {public String addBinary(String a, String b) {int alen = a.length();int blen = b.length();StringBuilder sb = new StringBuilder();add(alen - 1, blen - 1, 0, a, b, sb);return sb.toString();}private void add(int i, int j, int carry, String a, String b, StringBuilder sb) {if (i <0 && j < 0 && carry < 1) return;int m = i >= 0 ? (a.charAt(i) - '0') : 0;int n = j >= 0 ? (b.charAt(j) - '0') : 0;int total = m + n + carry;add(i - 1, j - 1, total / 2, a, b, sb);sb.append((char)(total % 2 +'0'));}public static void main(String[] args) {// TODO Auto-generated method stubAddBinary a = new AddBinary();System.out.println(a.addBinary("0", "0"));}}

LeetCode - Add Binary相关推荐

  1. LeetCode: Add Binary 解题报告

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

  2. LeetCode Add Binary

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

  3. [LeetCode]Add Binary

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

  4. [leetcode] Add Binary

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

  5. LeetCode:Add Binary

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

  6. LeetCode Add Binary(二进制加法)

    思路:大整数加法 代码如下: public class Solution {public String addBinary(String a, String b){StringBuilder sb = ...

  7. LeetCode刷题记录4——67. Add Binary(easy)

    LeetCode刷题记录4--67. Add Binary(easy) 目录 LeetCode刷题记录4--67. Add Binary(easy) 题目 语言 思路 后记 题目 今天这题是与字符串相 ...

  8. leetcode 67 Add Binary

    Add Binary Total Accepted: 46815 Total Submissions: 189215 My Submissions Given two binary strings, ...

  9. java两个二进制数求和,Leetcode 67:Add Binary(二进制求和)

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

最新文章

  1. Cloud.com今天正式上线了
  2. php-7.2.13的安装,php7.2 安装mcrypt扩展
  3. Array with Odd Sum CodeForces - 1296A
  4. C# ObservableCollection和List的区别
  5. 荔枝糖FPGA开发板相关博客
  6. matlab生成点的坐标,根据点的发展坐标,将点的轨迹画出来
  7. python网格搜索核函数_python机器学习——超参数网格搜索
  8. 【数据结构与算法】1.2 数据结构与算法分析
  9. 计算机本科科研什么项目,本科生做科研:大势所趋?
  10. 当下的力量实践手册读书笔记(1.28)
  11. OpenGauss/MogDB调用C FUNCTION 范例
  12. Python-opencv读取视频流处理后保存成mp4格式的视频源码
  13. 用天文方法计算二十四节气
  14. 头条号发视频有收益吗,头条号发视频就有收益吗
  15. 运行 Webpack 项目图片和favicon.ico找不到, 图片404错误
  16. mac 文字识别软件ocr_Mac平台上一款免费的OCR文字识别功能的屏幕截图软件Screen OCR...
  17. pandas dataframe rolling 移动计算
  18. 波导Z769手机java下载_手机指令秘籍传授
  19. Python爬虫爬取智联招聘(进阶版)
  20. python3模拟抓狐狸小游戏

热门文章

  1. Django--Uploaded Files以及Handlers
  2. 《精通Linux设备驱动程序开发》——1.5 Linux发行版
  3. “九头虫”病毒技术分析报告
  4. Linux下安装rlwrap
  5. ADO.NET Entity Framework如何:通过每种类型一个表继承以定义模型(实体框架)
  6. 无人驾驶油电混动牵引车_比纯电动更轻,省油率高达10%!解读首台国产非插电式混动卡车...
  7. python若干整数的最大值_实例讲解Python中整数的最大值输出
  8. C 语言编程 — 结构化程序流的汇编代码与 CPU 指令集
  9. 通过 vSphere WS API 获取 vCenter Datastore Provisioned Space 置备空间
  10. Microsoft Azure_Fabric