408. Add Binary【LintCode java】

來源:互聯網
上載者:User

標籤:i++   str   break   substring   render   bst   reverse   rip   ring   

Description

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

Example

a = 11

b = 1

Return 100

解題:二進位相加。我的思路是,先轉成StringBuilder對象(reverse方法比較好用),因為相加是從最後開始,所以先用reverse方法倒轉過來。和上一題類似,用carry變數表示進位,0為不進位,1為需要進一位。最後的結果再倒過來。具體細節標註在代碼中,代碼如下:

public class Solution {    /**     * @param a: a number     * @param b: a number     * @return: the result     */    public String addBinary(String a, String b) {        // write your code here        StringBuilder sa = new StringBuilder(a);        StringBuilder sb = new StringBuilder(b);        StringBuilder res = new StringBuilder();        int carry = 0;//表示進位        sa.reverse();        sb.reverse();        int i = 0;        for(i = 0; i < sa.length() && i < sb.length(); i++){            int tpa = sa.charAt(i) - ‘0‘;            int tpb = sb.charAt(i) - ‘0‘;            if(carry == 0){   // 沒有進位                if(tpa == 1 && tpb == 1){                    carry = 1;                    res.append(‘0‘);                }else{                    char temp = (char)((int)‘0‘ + (tpa+tpb));                    res.append( temp );                }            }else{  //有進位;                if(tpa + tpb == 1){                    carry = 1; // 依然有進位                    res.append(‘0‘);                }else if( tpa + tpb == 2){                    carry = 1;                    res.append(‘1‘);                }else{                    // 0 + 0                    carry = 0;                    res.append(‘1‘);                }            }        }        //對剩下的處理        while(i < sa.length()){            //把sa後面的接上去,但是要考慮進位            if(carry == 0){                res.append(sa.substring(i));                break;            }else{                //有進位                if(sa.charAt(i) == ‘1‘){                    res.append(‘0‘);                }else{                    res.append(‘1‘);                    carry = 0;                 }                i++;            }        }                while(i < sb.length()){            //把sa後面的接上去,但是要考慮進位            if(carry == 0){                res.append(sb.substring(i));                break;            }else{                //有進位                if(sb.charAt(i) == ‘1‘){                    res.append(‘0‘);                }else{                    res.append(‘1‘);                    carry = 0;                 }                i++;            }        }        if(carry == 1)            res.append(‘1‘);        return res.reverse().toString();    }}

 

 

408. Add Binary【LintCode java】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.