LeetCode Add Binary 两个二进制数相加
class Solution {
public:
string addBinary(string a, string b) {
if(a==""&&b=="") return "";
if(a=="") return b;
if(b=="") return a;
char *pa=&a[],*pb=&b[];
int na=,nb=;
int i=;
int t=,m=;
while(*pa!='\0'){
pa++;
na++;
}
pa--;
while(*pb!='\0'){
pb++;
nb++;
}
pb--;
if(na>=nb){ //存a中
for(i=na-;i>-;i--){
if(i>na-nb-){
m=(*pa-'') + (*pb-'')+t;
pa--;
pb--;
}
else if( i<na-nb &&t == )
return a;
else{
m=(*pa-'')+t;
pa--;
}
t=m/; //进位
m=m%; //当前位
a[i]=m+'';
}
if(t>)
return (""+a);
else
return a;
}
else{ //存b中
for(i=nb-;i>-;i--){
if(i>nb-na-){
m=(*pb-'') + (*pa-'')+t;
pa--;
pb--;
}
else if( i<nb-na &&t == )
return b;
else{
m=(*pb-'')+t;
pb--;
}
t=m/; //进位
m=m%; //当前位
b[i]=m+'';
}
if(t>)
return (""+b);
else
return b;
}
}
};
题意:给出两个字符串,里面分别存有一个二进制数,将二进制相加后返回结果,依然是字符串型。
思路:两个指针,从后面开始将两个二进制数相加,如果有一个比较长,那么另一个指针肯定先扫完一个字符串,此时将结果保存在较长的那个字符串中返回。在一个字符串已扫完的情况下,另一个字符串的未扫完部分扔要判断是否需要继续加,这取决于进位是否为0,若为0,连后面未扫部分都不用扫了,直接返回这个扔有未扫的字符串。若进位不为0,那么就要一个个继续扫下去了,只要满足一个条件就能不用继续往下扫,此条件是进位为0。若未扫完的那个字符串扫到完了,需再考虑一次进位。
吐槽:代码是草稿代码,但是AC了,从比较容易理解的角度写的。
LeetCode Add Binary 两个二进制数相加的更多相关文章
- [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 ...
- LeetCode: Add Binary 解题报告
Add BinaryGiven two binary strings, return their sum (also a binary string). For example,a = "1 ...
- [Leetcode] add binary 二进制加法
Given two binary strings, return their sum (also a binary string). For example,a ="11"b =& ...
- [leetcode]Add Binary @ Python
原题地址:https://oj.leetcode.com/problems/add-binary/ 题意: Given two binary strings, return their sum (al ...
- Leetcode Add Binary
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...
- LeetCode Add Binary |My Solution
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 = "1 ...
- LeetCode 面试:Add Binary
1 题目 Given two binary strings, return their sum (also a binary string). For example,a = "11&quo ...
随机推荐
- 利用css实现鼠标经过元素,下划线由中间向两边展开
代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- Python 爬虫笔记
urllib python3 与 python2 的urllib很不一样,之前urllib2的方法基本上都变成了python3里的urllib.request模块中 import urllib.req ...
- cf785D(组合数学)
题目链接: http://codeforces.com/problemset/problem/785/D 题意: 左边全为 '(' 右边全为 ')' 且两者数量想等的字符串称为 RSBS. 给出一个由 ...
- QuotaExceededError: The quota has been exceeded. localStorage缓存超出限制
今天在项目中遇到了一个问题,localStorage存储超出限制.报错信息如标题.这个是因为最近做了一波优化,把导航栏和一些用户信息本地化存储,都放在localStorage里,也不是每个用户会出现这 ...
- react native ios打包到真机,即生产包
参考文章:http://www.devio.org/2017/02/09/React-Native%E5%8F%91%E5%B8%83APP%E4%B9%8B%E6%89%93%E5%8C%85iOS ...
- VC添加全局热键的方法
VC添加全局热键的方法 这个方法靠谱 http://blog.csdn.net/lujianfeiccie2009/article/details/7498704 VC添加全局热键的方法 标签: bu ...
- return this链式操作
function Fn(){}; Fn.prototype = { constructor:Fn, a:function(){ alert(1); return this; //实现链式操作.即fn. ...
- 3、kvm配置vnc
配置kvm通过vnc访问 virsh edit privi-server 添加如下配置: <graphics type='vnc' port='5901' autoport='no' liste ...
- 自定义ClassLoader加载加密的class文件
package com.yd.wmsc.util; public class Test { public void say(){ System.out.println("Say Hello& ...
- 如何将拷贝过来的数据 *.ibd 文件生效
1.将拷贝的数据文件 "qqq.idb"放在自己的数据库中. 一般存放在 mysql/ data/ databasename 下 2. "qqq.idb" ...