问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3929 访问。

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

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

输入: a = "11", b = "1"

输出: "100"

输入: a = "1010", b = "1011"

输出: "10101"


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

The input strings are both non-empty and contains only characters 1 or 0.

Input: a = "11", b = "1"

Output: "100"

Input: a = "1010", b = "1011"

Output: "10101"


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3929 访问。

public class Program {

    public static void Main(string[] args) {
var a = "1010";
var b = "1011"; var res = AddBinary(a, b);
Console.WriteLine(res); Console.ReadKey();
} private static string AddBinary(string a, string b) {
//找出最长的加1,留1位进位
var max = Math.Max(a.Length, b.Length) + 1;
//按总长度补位,以便统一处理,否则要考虑边界
var a2 = a.PadLeft(max, '0');
var b2 = b.PadLeft(max, '0');
//定义list存中间计算结果
var list = new List<int>();
//进位标志
var carryFlag = false;
for(int i = max - 1; i >= 0; i--) {
var add = int.Parse(a2[i].ToString())
+ int.Parse(b2[i].ToString());
//计算当前位的值,如果之前进位标志为真,则额外+1
if(carryFlag) add++;
//大于等于2时,此时仍然需要进位,带入下一次循环
carryFlag = add >= 2;
//存入中间计算结果
list.Add(add % 2);
}
//定义结果
var res = string.Empty;
//反转,list是按逆序从低位到高位的
list.Reverse();
//遍历输出到res
list.ForEach(r => { res += r.ToString(); });
//取消最高位0
res = res.TrimStart('0');
//如果空了,返回0
if(res.Length == 0) res = "0";
//返回结果
return res;
} }

以上给出1种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3929 访问。

10101

分析:

设两个二进制字符串中较长的字符串的长度为 n ,那么以上算法的时间复杂度为:  。

C#LeetCode刷题之#67-二进制求和(Add Binary)的更多相关文章

  1. C#LeetCode刷题之#598-范围求和 II​​​​​​​(Range Addition II)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3881 访问. 给定一个初始元素全部为 0,大小为 m*n 的矩阵 ...

  2. 【leetcode刷题笔记】Minimum Depth of Binary Tree

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  3. 【leetcode刷题笔记】Maximum Depth of Binary Tree

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  4. C#LeetCode刷题之#704-二分查找(Binary Search)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3999 访问. 给定一个 n 个元素有序的(升序)整型数组 num ...

  5. [Swift]LeetCode67. 二进制求和 | Add Binary

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

  6. C#LeetCode刷题-数学

    数学篇 # 题名 刷题 通过率 难度 2 两数相加   29.0% 中等 7 反转整数 C#LeetCode刷题之#7-反转整数(Reverse Integer) 28.6% 简单 8 字符串转整数 ...

  7. C#LeetCode刷题-二分查找​​​​​​​

    二分查找篇 # 题名 刷题 通过率 难度 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组的中位数(Median of Two Sorted Arrays)-该题未达最优解 30 ...

  8. Java实现 LeetCode 67 二进制求和

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

  9. C#LeetCode刷题-字符串

    字符串篇 # 题名 刷题 通过率 难度 3 无重复字符的最长子串   24.6% 中等 5 最长回文子串   22.4% 中等 6 Z字形变换   35.8% 中等 8 字符串转整数 (atoi)   ...

  10. leetcode刷题目录

    leetcode刷题目录 1. 两数之和 2. 两数相加 3. 无重复字符的最长子串 4. 寻找两个有序数组的中位数 5. 最长回文子串 6. Z 字形变换 7. 整数反转 8. 字符串转换整数 (a ...

随机推荐

  1. Flink之对时间的处理

    window+trigger+watermark处理全局乱序数据,指定窗口上的allowedLateness可以处理特定窗口操作的局部事件时间乱序数据 1.流处理系统中的微批 Flink内部也使用了某 ...

  2. OSCP Learning Notes - Exploit(7)

    Pre-Exploit Password Attacks Tools: 1. ncrack Ncrack 0.6 ( http://ncrack.org )Usage: ncrack [Options ...

  3. 消除win10桌面图标的右下方小箭头

    很容易的小东西,在这里简单提一下 新建一个记事本,写下以下代码,改为.bat后缀,双击运行,然后箭头消失 reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Micro ...

  4. EF Code 如何输出sql语句

    首先写拷贝下面类 public class EFLoggerProvider : ILoggerProvider { public ILogger CreateLogger(string catego ...

  5. Mybatis(二)简化Mybatis实现数据库操作

    要操作的数据库: 一.与数据库对应的bean类 public class User { private String username; private String sex; private Str ...

  6. Redis简介与部署

    一.简介 Redis是什么?redis是一款基于BSD协议,开源的非关系型数据库(nosql数据库),作者是意大利开发者Salvatore Sanfilippo在2009年发布,使用C语言编写:red ...

  7. logrotate nginx日志切割

    1.安装 centos: yum -y install logrotate ubuntu: apt-get install -y logrotate 2. 配置文件 /etc/logrotate.co ...

  8. 使用ATOMac进行Mac自动化测试

    ATOMac简介 atomac是一个支持在mac上做自动化的python库,GitHub地址如下: https://github.com/pyatom/pyatom 安装 # Python2 sudo ...

  9. c语言大小写转化函数(包括字母和字符串)

    本憨憨忘了好几次了,这次一定记住他们! 首先大小写相差32.转换的话自己写函数也是可以写出来的. 1.字母 如果是字母转的话,用toupper(),tolower() 头文件是<ctype.h& ...

  10. 字节数组X中存放着 0~F共16个十六进制数,请将这些数以十六进制形式显示在屏幕上。

    问题 字节数组X中存放着 0~F共16个十六进制数,请将这些数以十六进制形式显示在屏幕上. 代码 data segment x db 0,1,2,3,4,5,6,7,8,9,0ah,0bh,0eh,0 ...