[Leetcode] add binary 二进制加法
Given two binary strings, return their sum (also a binary string).
For example,
a ="11"
b ="1"
Return"100".
题意:将两个以字符串形式保存的二进制数进行相加。
思路:其实不管是以数组、字符串形式,加或者乘(multiply strings),一般的思路都是从后往前计算,用一个中间变量保存相加或者相乘的结果(加法是一个变量int 或string就行,乘就要用到向量了)。本题而言,要注意的是,若是相加时其中长度较小的字符串变量完时,怎么办?这时,其实可以用中间变量在字符串各自存在的前提下加上对应值就行。还有一个就是:字符和数字之间的相互转换,可以通过加减'0'来实现。最后一个问题是:最高位存在进位的情况,其实可以最后根据中间变量的值来进行处理。代码如下:
class Solution {
public:
string addBinary(string a, string b)
{
string res="";
int aLen=a.size()-;
int bLen=b.size()-;
int carry=;
while(aLen>=||bLen>=)
{
if(aLen>=)
carry+=a[aLen--]-'';
if(bLen>=)
carry+=b[bLen--]-''; res=char(carry%+'')+res; //必须存在char不然,就会报错。恩,不懂。也可以是to_string(carry%2)+res;
carry/=;
}
if(carry==)
{
res=''+res;
} return res;
}
};
[Leetcode] add binary 二进制加法的更多相关文章
- leetCode 67.Add Binary (二进制加法) 解题思路和方法
Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...
- 【LeetCode每天一题】Add Binary(二进制加法)
Given two binary strings, return their sum (also a binary string).The input strings are both non-emp ...
- [leetcode]67. Add Binary 二进制加法
Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...
- # Leetcode 67:Add Binary(二进制求和)
Leetcode 67:Add Binary(二进制求和) (python.java) Given two binary strings, return their sum (also a binar ...
- LeetCode: Add Binary 解题报告
Add BinaryGiven two binary strings, return their sum (also a binary string). For example,a = "1 ...
- Add Strings大整数加法十进制求和 & Add Binary二进制求和
[抄题]: 以字符串的形式给出两个非负整数 num1 和 num2,返回 num1和 num2 的和. 比如一个50位+一个100位. 给定 num1 = "123",num2 = ...
- Leetcode 67 Add Binary 大数加法+字符串处理
题意:两个二进制数相加,大数加法的变形 大数加法流程: 1.倒置两个大数,这一步能使所有大数对齐 2.逐位相加,同时进位 3.倒置两个大数的和作为输出 class Solution { public: ...
- [LeetCode] Add Binary 二进制数相加
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...
- LeetCode——Add Binary
Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...
随机推荐
- 浅谈C#实现Web代理服务器的几大步骤
代理服务程序是一种广泛使用的网络应用程序.代理程序的种类非常多,根据协议不同可以分成HTTP代理服务程序.FTP代理服务程序等,而运行代理服务程序的服务器也就相应称为HTTP代理服务器和FTP代理服务 ...
- go学习笔记-变量和常量
变量和常量 变量 基本写法 //定义一个名称为"variableName",类型为"type"的变量 var variableName type //定义三个类 ...
- java 堆栈内存分析详解
计算机术语里面堆和栈代表不同的存储结构:stack-栈:heap-堆 所以java虚拟机(JVM)中堆和栈是两种内存 堆.栈对比 对比点 堆 栈 JVM中的功能 内存数据区 内存指令区 动静态 运行时 ...
- java练习题——类与对象
一.请依据代码的输出结果,自行总结Java字段初始化的规律 public static void main(String[] args) { InitializeBlockClass obj=new ...
- LeetCode:11. ContainerWithWater(Medium)
原题链接:https://leetcode.com/problems/container-with-most-water/description/ 题目要求:给定n个非负整数a1,a2,...,an ...
- Hadoop入门案列,初学者Coder
1.WordCount Job类: package com.simope.mr.wcFor; import org.apache.hadoop.conf.Configuration; import o ...
- php用GD库给图片添加水印
php用GD库给图片添加文字水印,整个代码比较简单,DEMO如下: <?php /*打开图片*/ //1.配置图片路径 $src = "aeroplane.jpg"; //2 ...
- Python初步
准备在工作之余看看Python的东西 收录一些资料 Python初学者(零基础学习Python.Python入门)常见问题:书籍推荐.资料.社区 http://blog.csdn.net/xiaowa ...
- request.getparameter() 获取中文出现乱码 问题
http请求是以ISO-8859-1的编码来传送url的 如果页面的content-type为utf-8,那么在发送请求时,会将字符转成utf-8后进行传送 如: 中 的UTF-8编码为:E4 B8 ...
- 用es6写一个分数库
es6发布后nodejs开始更新.最近写一些库发现新特性还是很好用的,于是回来写一个分数库练手. 对于es6本身 ... => 以及 array.includes 很简洁.class依然不是很顺 ...