LeetCode(67)题解: Add Binary
https://leetcode.com/problems/add-binary/
题目:
Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100".
思路:
数组模拟加法操做,注意首位进一的情况。
class Solution {
public:
string addBinary(string a, string b) {
string c="",d="";
if(a==""&&b=="")
return "";
int a_t=a.size(),b_t=b.size(),min_t=min(a_t,b_t),max_t=max(a_t,b_t);
int tmp_a,tmp_b,jinyi=;
for(int i=;i<max_t;i++){
if(i<a_t)
tmp_a=a[a_t-i-]-'';
else
tmp_a=;
if(i<b_t)
tmp_b=b[b_t-i-]-'';
else
tmp_b=;
c.push_back((tmp_a+tmp_b+jinyi)%+'');
if(tmp_a+tmp_b+jinyi>=)
jinyi=;
else
jinyi=;
}
if(jinyi==)
c.push_back('');
for(int i=c.size()-;i>=;i--)
d.push_back(c[i]);
return d;
}
};
LeetCode(67)题解: Add Binary的更多相关文章
- # Leetcode 67:Add Binary(二进制求和)
Leetcode 67:Add Binary(二进制求和) (python.java) Given two binary strings, return their sum (also a binar ...
- leetcode解题:Add binary问题
顺便把之前做过的一个简单难度的题也贴上来吧 67. Add Binary Given two binary strings, return their sum (also a binary strin ...
- leetcode笔记:Add Binary
一.题目描写叙述 Given two binary strings, return their sum (also a binary string). For example, a = "1 ...
- LeetCode(67) Add Binary
题目 Given two binary strings, return their sum (also a binary string). For example, a = "11" ...
- LeetCode算法题-Add Binary(Java实现)
这是悦乐书的第157次更新,第159篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第16题(顺位题号是67).给定两个二进制字符串,返回它们的总和(也是二进制字符串).输 ...
- LeetCode OJ:Add Binary(二进制相加)
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...
- [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings
这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...
- 67. Add Binary【LeetCode】
67. Add Binary Given two binary strings, return their sum (also a binary string). For example,a = &q ...
- LeetCode练题——67. Add Binary
1.题目 67. Add Binary——easy Given two binary strings, return their sum (also a binary string). The inp ...
随机推荐
- SPOJ-Matrices with XOR property,暴力打表!
Matrices with XOR property 应该先去看看这题的,补题的时候发现这题其实挺简单的.. 题意:n*m的格子用1-n*m的数去填,要求如果一个格子(i1,j1)与另外一个格子(i2 ...
- 自定义Title
xml: <jp.co.view.TitleLayout android:id="@+id/titleLayout" android:layout_width="m ...
- BZOJ 3230 相似子串 ——后缀数组
题目的Source好有趣. 我们求出SA,然后求出每一个后缀中与前面本质不同的字符串的个数. 然后二分求出当前的字符串. 然后就是正反两次后缀数组求LCP的裸题了. 要注意,这时两个串的起点可能会相同 ...
- BZOJ 1026: [SCOI2009]windy数 【数位dp】
Description windy定义了一种windy数.不含前导零且相邻两个数字之差至少为2的正整数被称为windy数. windy想知道,在A和B之间,包括A和B,总共有多少个windy数? In ...
- P3147 [USACO16OPEN]262144 (贪心)
题目描述 给定一个1*n的地图,在里面玩2048,每次可以合并相邻两个(数值范围1-262,144),问最大能合出多少.注意合并后的数值并非加倍而是+1,例如2与2合并后的数值为3. 这道题的思路: ...
- pageContext,request,session,application生命周期
equest是封装client端(也就是用户通过browser)提交的请求数据和属性的对象. response是封装web server端响应数据和属性的对象. 我们经常会将pageContext.r ...
- JavaScript 数组操作函数--转载+格式整理
JavaScript 数组操作函数(部分)--转载+格式整理 今天看了一篇文章,主要讲的对常用的Js操作函数:push,pop,join,shift,unshift,slice,splice,conc ...
- POJ 2185 Milking Grid [二维KMP next数组]
传送门 直接转田神的了: Milking Grid Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 6665 Accept ...
- Xcode打包应用为ipa
Xcode教程 Xcode4发布测试 打包Archive操作是本文要介绍的内容,发布测试的最后一步打包(Archive),Xcode4帮助文档有比较详细介绍,但是居然是错的,这里说明一下. 1.设置& ...
- Requests模拟登陆
requests模拟登陆知乎网站 实例 # -*- coding: utf-8 -*- __author__ = 'CQ' import requests try: import cookielib ...