Leetcode-67. Add Binary
来源:互联网 发布:哪个cms好 编辑:程序博客网 时间:2024/05/01 15:53
前言:为了后续的实习面试,开始疯狂刷题,非常欢迎志同道合的朋友一起交流。因为时间比较紧张,目前的规划是先过一遍,写出能想到的最优算法,第二遍再考虑最优或者较优的方法。如有错误欢迎指正。博主首发CSDN,mcf171专栏。
博客链接:mcf171的博客
——————————————————————————————
Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100"
.
public class Solution { public String addBinary(String a, String b) { StringBuffer sb = new StringBuffer("");int carryNumber = 0;int i = a.length() - 1,j = b.length() - 1;while(i >= 0 && j >= 0){int sum = (a.charAt(i) - '0') + (b.charAt(j) - '0') + carryNumber;carryNumber = sum / 2;sb.insert(0,sum%2);i --; j --;}while( i >= 0){int sum = (a.charAt(i) - '0') + carryNumber;carryNumber = sum/2;sb.insert(0,sum%2);i--;}while( j >=0 ){int sum = (b.charAt(j) - '0') + carryNumber;carryNumber = sum/2;sb.insert(0,sum%2);j--;}if( carryNumber !=0 ) sb.insert(0,1);return sb.toString(); }}
0 0
- [LeetCode]67.Add Binary
- LeetCode --- 67. Add Binary
- [Leetcode] 67. Add Binary
- [leetcode] 67.Add Binary
- 【leetcode】67. Add Binary
- [leetcode] 67.Add Binary
- [leetcode] 67. Add Binary
- 【Leetcode】67. Add Binary
- 67. Add Binary LeetCode
- 【LeetCode】67. Add Binary
- leetcode--67. Add Binary
- leetcode 67. Add Binary
- LeetCode 67. Add Binary
- LeetCode *** 67. Add Binary
- leetcode 67. Add Binary
- 【leetcode】67.Add Binary
- LeetCode 67. Add Binary
- LeetCode-67.Add Binary
- 拒绝从入门到放弃_《Python 核心编程 (第二版)》必读目录
- Android Studio 问题记录-全部java文件全部报错,却可以运行
- SQL符合函数查询
- 两个Activity之间重复相互跳转时遇到的坑
- 递归神经网络RNN原理——Elman网络原理——结合实例MATLAB(BPTT算法)实现
- Leetcode-67. Add Binary
- MySQL关于Value '0000-00-00 00:00:00' can not be represented as java.sql.Timestamp
- HDU 1671 Phone List
- oracle 下载地址(备忘)
- 389.leetcode Find the Difference(easy)[字符串处理]
- [LeetCode]--172. Factorial Trailing Zeroes
- SQL聚合函数查询
- Clojure 学习入门(4)- 字符串
- 洛谷 P3094 [USACO13DEC]假期计划Vacation Planning