LeetCode - 67. Add Binary(4ms)
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.
Example 1:
Input: a = "11", b = "1"
Output: "100"
Example 2:
Input: a = "1010", b = "1011"
Output: "10101"
class Solution {
public:
string addBinary(string a, string b) {
int la = a.length();
int lb = b.length();
string res = "";
int car = ;
if(la <= lb) {
for(int i = la - ; i >= ; i--) {
if(a[i] == '' && b[lb - (la - i)] == '') {
if(car == )
res = '' + res;
else {
res = '' + res;
car = ;
}
}
else if(a[i] != b[lb - (la - i)]) {
if(car == )
res = '' + res;
else
res = '' + res;
}
else {
if(car == ) {
res = '' + res;
car = ;
}
else
res = '' + res;
}
}
for(int i = lb - la - ; i >= ; i--) {
if(b[i] == '') {
if(car == )
res = '' + res;
else
res = '' + res;
}
else {
if(car == ) {
res = '' + res;
car = ;
}
else
res = '' + res;
}
}
if(car == ) {
res = '' + res;
}
return res;
}
else if(lb < la) {
for(int i = lb - ; i >= ; i--) {
if(b[i] == '' && a[la - (lb - i)] == '') {
if(car == )
res = '' + res;
else {
res = '' + res;
car = ;
}
}
else if(b[i] != a[la - (lb - i)]) {
if(car == )
res = '' + res;
else
res = '' + res;
}
else {
if(car == ) {
res = '' + res;
car = ;
}
else
res = '' + res;
}
}
for(int i = la - lb - ; i >= ; i--) {
if(a[i] == '') {
if(car == )
res = '' + res;
else
res = '' + res;
}
else {
if(car == ) {
res = '' + res;
car = ;
}
else
res = '' + res;
}
}
if(car == ) {
res = '' + res;
}
return res;
}
}
};
LeetCode - 67. Add Binary(4ms)的更多相关文章
- 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 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 ...
- (String) 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 二进制加法
Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...
- leetcode 67. Add Binary (高精度加法)
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...
- LeetCode 67 Add Binary(二进制相加)(*)
翻译 给定两个二进制字符串,返回它们的和(也是二进制字符串). 比如, a = "11" b = "1" 返回 "100". 原文 Give ...
- leetCode 67.Add Binary (二进制加法) 解题思路和方法
Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...
随机推荐
- [luoguP1443]马的遍历
首先来看一下题目描述: 题目描述 有一个n*m的棋盘(1<n,m<=400),在某个点上有一个马,要求你计算出马到达棋盘上任意一个点最少要走几步 输入输出格式 输入格式: 一行四个数据,棋 ...
- ListView与SimpleAdapter
Adapter可以视作控件与数据之间的桥梁 对ListView做自由布局和填充需要使用到Adapter,这里我们采用SimpleAdapter. 简单来说: 1.定义一个ListItem,其数据结构是 ...
- Retain NULL values vs Keep NULLs in SSIS Dataflows - Which To Use? (转载)
There is some confusion as to what the various NULL settings all do in SSIS. In fact in one team whe ...
- 微信小程序【消息推送服务器认证C# WebAPI】
参考微信开发文档: https://developers.weixin.qq.com/miniprogram/dev/api/custommsg/callback_help.html 代码可用 /// ...
- 获取APP地图权限
获取APP地图权限 NSLocationWhenUseUsageDescription,在info里面设置为空
- css3 笔记
1.元字符使用: []: 全部可选项 ||:并列 |:多选一 ?: 0个或者一个 *:0个或者多个 {}: 范围 2.CSS3属性选择器: E[attr]:存在attr属性即可: E[attr=val ...
- C/C++使用keybd_event模拟键盘按键
#include <stdio.h> #include <Windows.h> /* 设置键盘大小写状态 big:为TRUE则切换大写状态,否则切换小写状态 */ VOID M ...
- 二进制部署etcd集群
kuberntes 系统使用 etcd 存储所有数据,本文档介绍部署一个三节点高可用 etcd 集群的步骤,这三个节点配置复用 ,我在这里没有做认证,如果有需要也可以做. 下载二进制文件 到 http ...
- JSP/Servlet开发——第一章 动态网页基础
1.动态网页:在服务端运行的使用程序语言设计的交互网页 : ●动态网站并不是指具有动画功能的网站,而是指网站内容可根据不同情况动态变更的网站(股票网站),一般情况下动态网站通过数据库进行架构. ●动态 ...
- c# WebBrowser开发参考资料--杂七杂八
c# WebBrowser开发参考资料 http://hi.baidu.com/motiansen/blog/item/9e99a518233ca3b24aedbca9.html=========== ...