[leetcode] 67. Add Binary 解题报告
来源:互联网 发布:淘宝密码忘了 编辑:程序博客网 时间:2024/05/21 15:41
题目链接: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) { int len1 = a.size(), len2 = b.size(), flag = 0; string ans; while(len1 > 0 || len2 > 0) { int val = 0; if(len1 > 0) val += a[--len1] - '0'; if(len2 > 0) val += b[--len2] - '0'; ans.insert(ans.begin(), '0'+(val+flag)%2); flag = (val+flag)/2; } if(flag) ans = "1" + ans; return ans; }};
0 0
- [leetcode] 67. Add Binary 解题报告
- [Leetcode] 67. Add Binary 解题报告
- LeetCode-Add Binary-解题报告
- [LeetCode] Add Binary 解题报告
- 【LeetCode】Add Binary 解题报告
- Leetcode 67. Add Binary 二进制加法 解题报告
- Add Binary | leetcode 67 【Java解题报告】
- LeetCode-Add Digits-解题报告
- 【LeetCode解题报告】Add Digits
- 【LeetCode】 Add Digits 解题报告
- 【LeetCode】Add Strings 解题报告
- 【LeetCode】Binary Watch 解题报告
- leetCode 67.Add Binary (二进制加法) 解题思路和方法
- leetcode解题方案--067--Add Binary
- 【LeetCode】Add Two Numbers 解题报告
- LeetCode-Add Two Numbers-解题报告
- Leetcode (2) Add Two Numbers 解题报告
- [leetcode] 258. Add Digits 解题报告
- DP-----斐波纳契-杨辉三角--素数求法
- Java实现简单的DES加密解密
- MakeFile 文件
- 31.iPhone的磁场传感器简单介绍
- Beatifulsoup 中soup的get_text()方法
- [leetcode] 67. Add Binary 解题报告
- Linux Crontab 安装使用详细说明
- 23种设计模式(15)_行为型_迭代器模式(Iterator Pattern)
- Android中strings.xml中首字符或尾字符空格无作用
- Java数据类型
- Spring AOP快速入门
- 32.利用UIViewAnimation动画制作一个简单的下拉菜单
- LDAP-轻量目录访问协议
- 如何用好 Google 等搜索引擎?