LeetCode手记-Add Binary
问题描述
问题分析
分析题意,此题实际是求解两个二进制数的和,但是有两点要注意:
1.字符串的长度不限,所以相应十进制数值很可能会超过int的上限。
2.二进制的加法规则是自右向左进位,需要注意,以题目示例为例:
11
+ 1
------
100
所以直接将二进制字符串转成十进制值相加求和,再将十进制和转为二进制字符串的做法是不被接受的,虽然其复杂度只有O(1),错误做法如下;
public class Solution {
public string AddBinary(string a, string b) {
var result = string.Empty;
var sum = StringToInt(a) + StringToInt(b);
result = IntToString(sum);
return result;
}
public long StringToInt(string s)
{ if (s != null || s.Length > )
{
var a = Convert.ToInt32(s, );
return a;
}
return long.Parse(""); } public string IntToString(long i)
{
var b = string.Empty;
b= Convert.ToString(i, );
return b;
}
}
正确的做法应该是将二进制字符串转为字符集合,根据进算结果相应进位或者求和,这样可以忽略int上限,复杂度也仅为O(n),代码实现如下:
public class Solution {
public string AddBinary(string a, string b) {
var result = string.Empty;
var dataA = a.Reverse<char>().ToList();
var dataB = b.Reverse<char>().ToList();
var maxAry = dataA.Count > dataB.Count ? dataA : dataB;
var degree = ;
for (int i = ; i < maxAry.Count; i++)
{
var j = ;
var k = ;
if (i < dataA.Count)
{
j = dataA[i] - '';
}
if (i < dataB.Count)
{
k = dataB[i] - '';
}
var sum = j + k + degree;
switch (sum)
{
case :
maxAry[i] = '';
degree = ;
break;
case :
maxAry[i] = '';
degree = ;
break;
case :
maxAry[i] = '';
degree = ;
break;
case :
maxAry[i] = '';
degree = ;
break;
} }
if (degree == )
{
maxAry.Add('');
} maxAry.Reverse(); for (int k = ; k < maxAry.Count; k++)
{
result += maxAry[k];
}
return result; } }
其中值得一提的是,我将容器由Array换成List之后,速度提升了近30ms,跑出了此题C#解答的最好纪录。
LeetCode手记-Add Binary的更多相关文章
- LeetCode 面试:Add Binary
1 题目 Given two binary strings, return their sum (also a binary string). For example,a = "11&quo ...
- LeetCode 67. Add Binary (二进制相加)
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...
- [LeetCode] 67. Add Binary 二进制数相加
Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...
- 【leetcode】Add Binary
题目简述: Given two binary strings, return their sum (also a binary string). For example, a = "11&q ...
- Java for LeetCode 067 Add Binary
Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...
- LeetCode 67. Add Binary
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...
- Java [Leetcode 67]Add Binary
题目描述: Given two binary strings, return their sum (also a binary string). For example,a = "11&qu ...
- LeetCode(56)-Add Binary
题目: Given two binary strings, return their sum (also a binary string). For example, a = "11&quo ...
- (String) leetcode 67. Add Binary
Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...
随机推荐
- JAVA中的配置文件XML
一:概念 1.XML Extensible markup Language 可拓展标记语言 2.功能:存储数据(配置文件,在网络中传输数据) 3.html和xml的区别 3.1xml标记全是自定义的 ...
- Kivy 从memory 读取image
借助PIL来处理的图片数据 fp = BytesIO() img = Image.frombytes('RGB', img_size, buf_bytes, 'raw', 'BGR;16', 0, 1 ...
- Linux下安装PHP环境并配置Nginx支持php-fpm模块
修改php配置 vi /etc/php.ini 打开php配置文件/etc/php.ini找到cgi.fix_pathinfo配置项,这一项默认被注释并且值为1,根据官方文档的说明,这里为了当文件不存 ...
- python生成器(generator)、迭代器(iterator)、可迭代对象(iterable)区别
三者联系 迭代器(iterator)是一个更抽象的概念,任何对象,如果它的类有next方法(next python3)和__iter__方法返回自己本身,即为迭代器 通常生成器是通过调用一个或多个yi ...
- 目前php连接mysql的主要方式
mysqli和PDO, 其中mysqli可以有面向过程,面向对象两种方式.而pdo只有面向对象的方式. <?php // $mysql_server = "localhost" ...
- django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call
Error info: django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, ...
- Linux驱动之异常处理体系结构简析
异常的概念在单片机中也接触过,它的意思是让CPU可以暂停当前的事情,跳到异常处理程序去执行.以前写单片机裸机程序属于前后台程序,前台指的就是mian函数里的while(1)大循环,后台指的就是产生异常 ...
- 通过github安装crawley出现的问题
http://www.cnblogs.com/hbwxcw/p/7086188.html
- Mysql数据库 (JTree应用)
package com.databases.jtree; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt ...
- Java-static关键字解析
static关键字是很多朋友在编写代码和阅读代码时碰到的比较难以理解的一个关键字,也是各大公司的面试官喜欢在面试时问到的知识点之一.下面就先讲述一下static关键字的用法和平常容易误解的地方,最后列 ...