每天 3 分钟,走上算法的逆袭之路。

前文合集

每日一道 LeetCode 前文合集

代码仓库

GitHub: https://github.com/meteor1993/LeetCode

Gitee: https://gitee.com/inwsy/LeetCode

题目:数组加一

题目来源:https://leetcode-cn.com/problems/add-binary/

给你两个二进制字符串,返回它们的和(用二进制表示)。

输入为 非空 字符串且只包含数字 1 和 0。

示例 1:

  1. 输入: a = "11", b = "1"
  2. 输出: "100"

示例 2:

  1. 输入: a = "1010", b = "1011"
  2. 输出: "10101"

提示:

  • 每个字符串仅由字符 '0' 或 '1' 组成。
  • 1 <= a.length, b.length <= 10^4
  • 字符串如果不是 "0" ,就都不含前导零。

解题过程

方案一:偷鸡方法

每天做题就像一个开盲盒的过程,没开始之前,永远都不知道会遇到什么样的题目。

这道题我第一眼看过去,二进制加法?不会,我才不要自己去写一个二进制加法出来, Java 给我们提供了现成的 math 函数包,是用来看的么?

果断第一个想法是先把二进制转成十进制,做完加法以后再转回去做输出。

我就是个小机灵鬼。

  1. public String addBinary(String a, String b) {
  2. return decimalToBinary(binaryToDecimal(a).add(binaryToDecimal(b)));
  3. }
  4. // 定义二进制转十进制
  5. private BigInteger binaryToDecimal(String binarySource) {
  6. return new BigInteger(binarySource, 2);
  7. }
  8. // 定义十进制转二进制
  9. private String decimalToBinary(BigInteger decimalSource) {
  10. return decimalSource.toString(2);
  11. }

代码上还可以精简一点写成一行,我是怕有同学看不懂,另外定义了两个方法,比如:

  1. public String addBinary(String a, String b) {
  2. return (new BigInteger(a, 2).add(new BigInteger(b, 2))).toString(2);
  3. }

结果扔到 LeetCode 上去运行,直接给我报了个编译错误。

这个意思应该是不支持 BigInteger() 函数,难道我在前面加个导包?

  1. import java.math.BigInteger;
  2. class Solution {
  3. public String addBinary(String a, String b) {
  4. return (new BigInteger(a, 2).add(new BigInteger(b, 2)).toString(2));
  5. }
  6. }

果然添加了导包以后就正常了,就是这个执行耗时有点惨不忍睹。

果然每次取巧的方案最后消耗时长都是最坑的,还是老老实实的想办法自己实现一下二进制加法吧。

方案二:二进制加法

二进制加法和十进制是一样的,都是从低位开始往高位运算,只不过十进制是满 10 进 1 ,而二进制是满 2 进 1 。

我们接下来要做的就是模拟一下二进制的加法过程,从低位开始计算,并且实现满 2 进 1 这个操作。

  1. public String addBinary_1(String a, String b) {
  2. StringBuilder sb = new StringBuilder();
  3. // 定义进位
  4. int pre = 0;
  5. for (int i = a.length() - 1, j = b.length() - 1; i >= 0 || j >= 0; i--, j--) {
  6. int sum = pre;
  7. if (i >= 0) sum += a.charAt(i) - '0';
  8. if (j >= 0) sum += b.charAt(j) - '0';
  9. // 当前位添加至 sb
  10. sb.append(sum % 2);
  11. // 计算进位
  12. pre = sum / 2;
  13. }
  14. // 进位如果为 1 则添加到 sb 上
  15. if (pre == 1) sb.append('1');
  16. // 反转字符串输出
  17. return sb.reverse().toString();
  18. }

上面这个算法其中有一点需要注意,就是为什么要做 a.charAt(i) - '0' 这样一步操作,因为直接通过 a.charAt(i) 取出来的是当前字符的 ASCII 值, 0 的 ASCII 值是 48 ,而 1 的 ASCII 值是 49 ,用这两个值都去减 '0' ,正好得到了我们需要的 1 或者 0 。

其余的计算当前位是取模,计算进位是做除法,这两个就不多说了,很常规的用法。

每日一道 LeetCode (15):二进制求和的更多相关文章

  1. 每日一道 LeetCode (41):阶乘后的零

    每天 3 分钟,走上算法的逆袭之路. 前文合集 每日一道 LeetCode 前文合集 代码仓库 GitHub: https://github.com/meteor1993/LeetCode Gitee ...

  2. LeetCode:二进制求和【67】

    LeetCode:二进制求和[67] 题目描述 给定两个二进制字符串,返回他们的和(用二进制表示). 输入为非空字符串且只包含数字 1 和 0. 示例 1: 输入: a = "11" ...

  3. 每日一道 LeetCode (3):回文数

    前文合集 每日一道 LeetCode 文章合集 题目:回文数 题目来源:https://leetcode-cn.com/problems/palindrome-number/ 判断一个整数是否是回文数 ...

  4. 每日一道 LeetCode (5):最长公共前缀

    前文合集 每日一道 LeetCode 前文合集 代码仓库 GitHub: https://github.com/meteor1993/LeetCode Gitee: https://gitee.com ...

  5. 每日一道 LeetCode (6):有效的括号

    每天 3 分钟,走上算法的逆袭之路. 前文合集 每日一道 LeetCode 前文合集 代码仓库 GitHub: https://github.com/meteor1993/LeetCode Gitee ...

  6. 每日一道 LeetCode (8):删除排序数组中的重复项和移除元素

    每天 3 分钟,走上算法的逆袭之路. 前文合集 每日一道 LeetCode 前文合集 代码仓库 GitHub: https://github.com/meteor1993/LeetCode Gitee ...

  7. 每日一道 LeetCode (9):实现 strStr()

    每天 3 分钟,走上算法的逆袭之路. 前文合集 每日一道 LeetCode 前文合集 代码仓库 GitHub: https://github.com/meteor1993/LeetCode Gitee ...

  8. 每日一道 LeetCode (10):搜索插入位置

    每天 3 分钟,走上算法的逆袭之路. 前文合集 每日一道 LeetCode 前文合集 代码仓库 GitHub: https://github.com/meteor1993/LeetCode Gitee ...

  9. 每日一道 LeetCode (14):数组加一

    每天 3 分钟,走上算法的逆袭之路. 前文合集 每日一道 LeetCode 前文合集 代码仓库 GitHub: https://github.com/meteor1993/LeetCode Gitee ...

随机推荐

  1. markdownpad2初使用

      本来是想在csdn上写blog的,到那时不知道为什么,那个写blog的界面总是崩溃,写了半天的东西和公式也都没有了,很气愤,所以就准备下载一个本地的markdown编辑器,下载课两款一款是mark ...

  2. noi linux gedit 配置(c++环境)

    基本配置 方法一 查看所有命令: gsettings list-recursively | grep -i gedit 命令解释 gsettings set org.gnome.gedit.prefe ...

  3. 基于python实现查询ip地址来源

    接口调用方法是在url后面直接加上IP地址. url = 'http://freeapi.ipip.net/218.192.3.42' #中文免费 url2 = 'http://ip-api.com/ ...

  4. 深入理解golang:sync.map

    疑惑开篇 有了map为什么还要搞个sync.map 呢?它们之间有什么区别? 答:重要的一点是,map并发不是安全的. 在Go 1.6之前, 内置的map类型是部分goroutine安全的,并发的读没 ...

  5. git上传本地项目到gitlab

    <1>输入git config --global user.name "你的用户名" <2>输入git config --global user.email ...

  6. vue学习(十四) 条件搜索框动态查询表中数据 数组的新方法

    //html <div id="app"> <label> 名称搜索关键字: <input type="text" clasa=& ...

  7. 给隔壁的妹子讲『一个SQL语句是如何执行的?』

    前言 SQL作为Web开发是永远离开不的一个话题,天天写SQL,可是你知道一个SQL是如何执行的吗? select name from user where id = 1; 上面是一个简单的查询语句, ...

  8. C语言学习笔记二---数据类型运算符与表达式

    一.C的基本语法单位 1.标识符:有效长度:31(DOS环境下) 2.关键字:main不是 3.分隔符:空格符,制表符,换行符,换页符 4.注释符:a./*.....*/   b.// 二.C的常用输 ...

  9. LevelDB,你好~

    LevelDB,你好~ 上篇文章初识:LevelDB介绍了啥是LevelDB,LevelDB有啥特性,以及Linux环境下编译,使用及调试方法. 这篇文章的话,算是LevelDB源码学习的开端吧,主要 ...

  10. 安装phpldapadmin

    关闭防火墙 [root@ldapmaster2 ~]# systemctl disable firewall [root@ldapmaster2 ~]# systemctl stop firewall ...