【www.30064.com】LeetCode:67. Add Binary,leetcodebinary

LeetCode:67. Add Binary,leetcodebinary

题目:

Given two binary strings, return their sum (also a binary string).

For example,
a = "11"
b = "1"
Return "100".

那道难点是贰个字符串管理人位上实行演算,不清楚Python密闭好了,实现起来会不会相比费力。Java这里假如得以像C/C++
直接实行二进制运算的话,也是一处思路。作者刚开头一上手做起来实在是以为不太会,其实正是协调去落到实处二个二进制加法,再协作模2,余2,领会好进位的持筹握算。再贴二个官方论坛里的解题代码。

Solution:

【www.30064.com】LeetCode:67. Add Binary,leetcodebinary。public String addBinary(String a, String b) {
     StringBuilder sb = new StringBuilder();

    int i = a.length() -1 ;
     int j = b.length() – 1;
     int sum,carry = 0;

    while (i >= 0 || j >= 0) {
         sum = carry;

        if ( i >= 0 ) sum += a.charAt(i–) – ‘0’ ;
         if ( j >= 0 ) sum += b.charAt(j–) – ‘0’ ;

        sb.append(sum % 2);

        carry = sum / 2;
     }

    if (carry != 0) sb.append(carry);

    return sb.reverse().toString();

}

. Add Binary,leetcodebinary 标题:
Given two binary strings, return their sum (also a binary string). For
example, a = “11” b = “1” Return “100” . 那道标题是一个…

题目:

package addBinary67;
/*
Given two binary strings, return their sum (also a binary string).
For example,
a = “11”
b = “1”
Return “100”.
*/
www.30064.com ,public class Solution {
public static String addBinary(String a, String b) {
//ensure a.length()<=b.length()
if (a.length()>b.length())
return addBinary(b,a);
char[] chara=a.toCharArray();
char[] charb=b.toCharArray();
int lena=chara.length;
int lenb=charb.length;
StringBuilder sb=new StringBuilder();
int carry=0;
//add the same length numbers
for (int i=0;i<lena;i++){
int inta=chara[lena-1-i]-‘0’;
int intb=charb[lenb-1-i]-‘0’;
sb.append(inta^intb^carry);
carry=(inta&intb)|((inta^intb)&carry);
}
//add the longer length numbers
for (int i=lenb-lena-1;i>=0;i–){
int intb=charb[i]-‘0’;
sb.append(intb^carry);
carry=intb&carry;
}
//judge the first bit
if(carry>0)
sb.append(carry);
return sb.reverse().toString();

67. Add Binary,67addbinary

Given two binary strings, return their sum (also a binary string).

For example,
a = "11"
b = "1"
Return "100".

主题素材:求2个二进制字符串的和,结果也要二进制字符串输出。

 1 char* addBinary(char* a, char* b) {
 2     int i = strlen(a)-1;
 3     int j = strlen(b)-1;
 4     int c = 0;
 5     char temp;
 6     char *p = (char*)malloc(i > j ? i+3 :j+3);         //分配字符串空间
 7     char *s = p;
 8     while(i >= 0 || j >= 0 || c == 1)
 9     {
10         c += i >= 0 ? a[i--] - 48 : 0;                  //字符串每一位相加
11         c += j >= 0 ? b[j--] - 48 : 0;
12         *s++ = c % 2 + 48;                              
13         c = c/2;                                        //根据c的值每次要进位
14     }
15     *s = '\0';
16     i = 0;
17     j = strlen(p)-1;
18     while(i < j)                                      //反转字符串
19     {
20         temp = p[j];
21         p[j] = p[i];
22         p[i] = temp;
23         i++;
24         j--;
25     }
26     return p;
27 }

 

. Add Binary,67addbinary Given two binary
strings, return their sum (also a binary string). For example, a = “11”
b = “1” Return “100” . 标题:求2个二进制字符串的和,…

Given two binary strings, return their sum (also a binary string).

}
public static void main(String[] args) {
// TODO Auto-generated method stub
String
a=”10100000100100110110010000010101111011011001101110111111111101000000101111001110001111100001101″;
String
b=”110101001011101110001111100110001010100001101011101010000011011011001011101111001100000011011110011″;
//String a=”101″;
//String b=”10111″;
System.out.println(addBinary(a,b));
//110111101100010011000101110110100000011101000101011001000011011000001100011110011010010011000000000

For example,
a = "11"
b = "1"
Return "100".

}

那道标题是二个字符串管理人位上开始展览演算,不清楚Python密封好了,完毕起来会不会相比麻烦。Java这里要是得以像C/C++
直接实行二进制运算的话,也是一处思路。笔者刚开首一上手做起来的确是认为不太会,其实正是友善去达成二个二进制加法,再同盟模2,余2,精通好进位的臆度。再贴三个官方论坛里的解题代码。

}

Solution:

public String addBinary(String a, String b) {
     StringBuilder sb = new StringBuilder();

    int i = a.length() -1 ;
     int j = b.length() – 1;
     int sum,carry = 0;

    while (i >= 0 || j >= 0) {
         sum = carry;

        if ( i >= 0 ) sum += a.charAt(i–) – ‘0’ ;
         if ( j >= 0 ) sum += b.charAt(j–) – ‘0’ ;

        sb.append(sum % 2);

        carry = sum / 2;
     }

    if (carry != 0) sb.append(carry);

    return sb.reverse().toString();

}

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图