67. Add Binary
来源:互联网 发布:免费加入团队软件 编辑:程序博客网 时间:2024/05/22 16:05
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 ret = ""; int i = a.length() - 1; int j = b.length() - 1; int curSum = 0; //从右往左(低位往高位)逐位相加。 while(i >= 0 || j>=0 || curSum == 1) { //如果当前的索引大于等于零,则将当前为的数字加上,否则加上0 curSum += i>=0?a[i--]-'0':0; curSum += j>=0?b[j--]-'0':0; //将当前为的结果mod2之后加入结果字符串中 ret = (char)(curSum %2 + '0') +ret; //进位 curSum /=2; } return ret; }};
1
0 0
- [LeetCode]67.Add Binary
- LeetCode --- 67. Add Binary
- [Leetcode] 67. Add Binary
- [leetcode] 67.Add Binary
- 67. Add Binary
- 【leetcode】67. Add Binary
- 67. Add Binary
- [leetcode] 67.Add Binary
- [leetcode] 67. Add Binary
- 67. Add Binary
- 67. Add Binary
- 【Leetcode】67. Add Binary
- 67. Add Binary
- 67. Add Binary LeetCode
- 67. Add Binary
- 【LeetCode】67. Add Binary
- 67. Add Binary
- 67. Add Binary
- jdbc
- android在xml文件中定义drawable数组、id数组等
- 微信分销系统微商城网站
- AndroidMultitool3.5.6 批量打渠道包
- window.close()提示 "Scripts may close only the windows that were opened by it"
- 67. Add Binary
- Object-C NSTimer定时器使用
- GRE填空的注意要点
- 【腾讯bugly干货分享】精神哥手把手教你如何智斗ANR
- 启动eclipse错误:Failed to write core dump
- android-Ultra-Pull-To-Refresh Dem实例 Eclipse中编译的办法
- android平台led开发之硬件抽象层 (二)
- linux rar将文件解压到制定目录
- 到哪里找PPT所需图标?