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

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

SOLUTION:

指针指到两个字符串的末尾,不断往前推进,用carry表示进位。用stringbuilder来记录结果。

使用insert(0, c)函数将加出的结果不断插入到STRINGBUILDER.

 1 public class Solution {
 2     public String addBinary(String a, String b) {
 3         if (a == null || b == null) {
 4             return null;
 5         }
 6
 7         if (a.length() == 0) {
 8             return b;
 9         }
10
11         if (b.length() == 0) {
12             return a;
13         }
14
15         StringBuilder sb = new StringBuilder();
16
17         int p1 = a.length() - 1;
18         int p2 = b.length() - 1;
19
20         int carry = 0;
21         while (p1 >= 0 || p2 >= 0) {
22             int sum = carry;
23             if (p1 >= 0) {
24                 sum += (a.charAt(p1) - '0');
25             }
26
27             if (p2 >= 0) {
28                 sum += (b.charAt(p2) - '0');
29             }
30
31             char c = sum % 2 == 1 ? '1': '0';
32             sb.insert(0, c);
33             carry = sum / 2;
34
35             p1--;
36             p2--;
37         }
38
39         if (carry == 1) {
40             sb.insert(0, '1');
41         }
42
43         return sb.toString();
44     }
45 }

View Code

GITHUB CODE:

AddBinary.java

转载于:https://www.cnblogs.com/yuzhangcmu/p/4052845.html

LeetCode: Add Binary 解题报告相关推荐

  1. LeetCode - Add Binary

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

  2. LeetCode: Sort List 解题报告

    Sort List Sort a linked list in O(n log n) time using constant space complexity. 使用Merge Sort, 空间复杂度 ...

  3. Leetcode Weekly 188 解题报告

    文章目录 Leetcode 1441. 用栈操作构建数组 Leetcode 1442. 形成两个异或相等数组的三元组数目 Leetcode 1443. 收集树上所有苹果的最少时间 Leetcode 1 ...

  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" b ...

  6. [leetcode] Add Binary

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

  7. [LeetCode] Multiply Strings 解题报告

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  8. LeetCode:Add Binary

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

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

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

最新文章

  1. [CQOI2010]扑克牌
  2. SSRS - 请求因 HTTP 状态 401 失败: Unauthorized。
  3. oracle cache keep pool,请问:alter table ……storage(buffer_pool keep) 与cache的区别
  4. Paper:《Adam: A Method for Stochastic Optimization》的翻译与解读
  5. java基础语法以及进制的转换
  6. 跟小段一起学Solaris(20)---ipFilter防火墙
  7. Vue.js过滤器概述
  8. 赚大钱必备 怎样成为赚钱高手(图)
  9. Solaris 的防火墙ipfilter设置
  10. android自动化测试录制,(二)屏幕录制在android自动化测试中应用2015.06.29
  11. virtualbox+vagrant学习-3-Vagrant Share-5-Security
  12. scrapy自定义Request的缓存策略(减少内存占用)
  13. rancher中添加用户,赋予权限
  14. 建立SAP Router后,开放SAP访问的步骤
  15. 最常用常见通用字体有哪些
  16. 单片机之汇编语言和C语言(以PIC单片机为例)
  17. 停车、投票、领证,区块链如何在「智慧城市」建设中大显身手?
  18. 智能工厂具体的名词解释
  19. 【linux安装opencv3.4报错】ib/libopencv_videoio.so.3.4.2: undefined reference to `avcodec_get_context_defa
  20. DHCP协议详解及DHCP服务的配置

热门文章

  1. ccd相机好修吗_CCD到底值不值得买,CCD相机入坑全过程
  2. linux bogomips,内核探索:Linux BogoMips 探秘
  3. sql union 行数不同_十八般武艺玩转GaussDB(DWS)性能调优(二):坏味道SQL识别
  4. SpringBoot使用@Scheduled创建定时任务
  5. Java经纬度坐标转换到平面坐标
  6. java中怎样任意跳转到指定行而不受循环语句限制
  7. com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused
  8. java查看当前活动的线程数量
  9. ORACLE查询保留字
  10. Android开发笔记(一百四十)Word文件的读取与显示