67. Add BinaryLeetCode
Posted 皓浩浩皓
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了67. Add BinaryLeetCode相关的知识,希望对你有一定的参考价值。
67. Add Binary
Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100"
.
1 public class Solution { 2 public String addBinary(String a, String b) { 3 String res =""; 4 int l1=a.length()-1; 5 int l2=b.length()-1; 6 7 int carry=0; 8 for(int i=l1,j=l2;i>=0||j>=0;i--,j--){ 9 int sum=carry; 10 sum+=(i>=0)?(int)(a.charAt(i)-‘0‘):0; 11 sum+=(j>=0)?(int)(b.charAt(j)-‘0‘):0; 12 res =sum%2+res; 13 carry=sum/2; 14 15 } 16 if(carry!=0){ 17 res=carry+res; 18 } 19 return res; 20 } 21 }
以上是关于67. Add BinaryLeetCode的主要内容,如果未能解决你的问题,请参考以下文章
[HTML5] Add an SVG Image to a Webpage and Get a Reference to the Internal Elements in JavaScript(代码片