LEETCODE-Add Binary
来源:互联网 发布:各层网络协议 编辑:程序博客网 时间:2024/06/06 03:01
leetcode
Add Binary:
Given two binary strings, return their sum (also a binary string).
For example,
a = “11”
b = “1”
Return “100”.
#include<stdio.h>#include<iostream>#include<cstring>#define MAXN 300+10using namespace std;string Add(string a,string b){ if(a[0] == 48 &&b[0] == 48) return a; else{ int num1[MAXN] = {0},num2[MAXN] = {0}; int len1 = a.size(); int len2 = b.size(); int i=0,j=0; for(i = len1-1,j = 0;i >= 0;i--) num1[j++] = a[i] - '0'; for(i = len2-1,j = 0;i >= 0;i--) num2[j++] = b[i]-'0'; for(i = 0;i < MAXN;i++) { num1[i] += num2[i]; if(num1[i] >= 2) { num1[i] -=2; num1[i+1]++; } } int wh = MAXN-1; while(num1[wh] == 0) wh--; string c; for(int k = wh;k >= 0;k--) c += num1[k] + '0'; a = c; return a; printf("\n"); }}int main(){ string s1,s2; cin >> s1; cin >> s2; string s3; s3 = Add(s1,s2); cout << s3; return 0;}
对string类型的数据认识还不够完全,以后还要继续深入了解(准备入手一本C++primer)。
另外在写代码的过程中出现很多“小插曲”,用心写代码,杜绝低级错误。
之前浪费了很多时间没有抓住机会提高自己的能力,现在我要好好写代码,找到代码的乐趣。
https://leetcode.com/submissions/detail/38375877/
0 0
- LeetCode: Add Binary
- [Leetcode] Add Binary
- LeetCode Add Binary
- Leetcode: Add Binary
- [Leetcode] Add Binary
- LeetCode : Add Binary
- LeetCode Add Binary
- [LeetCode] Add Binary
- leetcode 58: Add Binary
- leetcode Add Binary
- [LeetCode] Add Binary
- 【leetcode】Add Binary
- [LeetCode]Add Binary
- [leetcode]Add Binary
- LeetCode-Add Binary
- [leetcode] Add Binary
- LeetCode - Add Binary
- leetcode:Add Binary
- 进制转换,16到10,10进制到12进制_华为SO挑战赛试题2015年8月
- 优秀项目经理应备技能08-项目管理的技能
- Mysql主从复制与读写分离闲谈
- Handle 消息传递机制知识点汇总
- Python--生成器
- LEETCODE-Add Binary
- Windows支持的4种类型的同步对象:临界区、互斥量、事件和信号量
- A1 = ? HDU 杭电2086 【数学】
- Mac下git的安装与配置
- 软件测试学习随记
- Cglib 与 JDK动态代理的运行性能比较
- 24个设计模式学习
- Cocos2d-x中Vector容器以及实例介绍
- 利用C语言进行urldecode,解决浏览器中的urlencode