当前位置:编程学习 > JAVA >>

Add Binary @LeetCode

package Level2;

/**
 * Add Binary
 *
 * Given two binary strings, return their sum (also a binary string).

For example,
a = "11"
b = "1"
Return "100".
 */
public class S67 {

	public static void main(String[] args) {
		String a = "1111";
		String b = "1111";
		System.out.println(addBinary(a, b));
	}
	
	public static String addBinary(String a, String b) {
        int i = a.length() - 1;		// i指向a的末尾
        int j = b.length() - 1;		// j指向b的末尾
        int c = 0;		// carry 进位
        // 先把String转为char数组便于处理
        char[] achar = a.toCharArray();
        char[] bchar = b.toCharArray();
        // 结果数组
        char[] reschar = new char[Math.max(achar.length, bchar.length)+2];
        int k = 0;		// k指向结果数组的开头
        
        while(true){
        	if(i<0 && j<0 && c==0){
        		break;
        	}
        	
        	int aint = 0;
        	int bint = 0;
        	
        	if(i >= 0){
        		aint = achar[i] - '0';
        	}
        	if(j >= 0){
        		bint = bchar[j] - '0';
        	}
        	if(aint + bint + c > 1){
        		reschar[k] = (char) ('0' + aint + bint + c - 2);
        		c = 1;
        	}else{
        		reschar[k] = (char) ('0' + aint + bint + c);
        		c = 0;
        	}
        	k++;
        	i--;
        	j--;
        }
         // char数组转string,然后翻转
        return new StringBuffer(new String(reschar, 0, k)).reverse().toString();
    }

}

 

补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,