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】的更多相关文章

  1. 365. Count 1 in Binary【LintCode java】

    Description Count how many 1 in binary representation of a 32-bit integer. Example Given 32, return  ...

  2. 376. Binary Tree Path Sum【LintCode java】

    Description Given a binary tree, find all paths that sum of the nodes in the path equals to a given ...

  3. 375. Clone Binary Tree【LintCode java】

    Description For the given binary tree, return a deep copy of it. Example Given a binary tree: 1 / \ ...

  4. 372. Delete Node in a Linked List【LintCode java】

    Description Implement an algorithm to delete a node in the middle of a singly linked list, given onl ...

  5. 245. Subtree【LintCode java】

    Description You have two very large binary trees: T1, with millions of nodes, and T2, with hundreds ...

  6. 227. Mock Hanoi Tower by Stacks【LintCode java】

    Description In the classic problem of Towers of Hanoi, you have 3 towers and N disks of different si ...

  7. 451. Swap Nodes in Pairs【LintCode java】

    Description Given a linked list, swap every two adjacent nodes and return its head. Example Given 1- ...

  8. 445. Cosine Similarity【LintCode java】

    Description Cosine similarity is a measure of similarity between two vectors of an inner product spa ...

  9. 433. Number of Islands【LintCode java】

    Description Given a boolean 2D matrix, 0 is represented as the sea, 1 is represented as the island. ...

随机推荐

  1. .NET获取IIS7.0及以上版本托管服务信息

    近期写了个扫描IIS托管站点然后定期注册到Consul的小工具,随意网上拷贝了个帮助类,搞完本机测试没问题,扔到服务器发现硕大的一个异常.. System.Runtime.InteropService ...

  2. 使用 JLINK 的 RTT 功能 进行 调试打印数据

    jlink V9 时,在 SWD 接口 模式 时  ,要 接 SWO 这个引脚 ,否则导致 在 FreeRTOS的任务中不能使用,  正确的 接线方法 是  VCC,GND,SWDIO,SWCLK,S ...

  3. 设计模式 - 抽象工厂模式(abstract factory pattern) 具体解释

    抽象工厂模式(abstract factory pattern) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details/2709 ...

  4. 仿手机iPhone QQ消息小红点动画1

    前言 偶然发现iPhone QQ 显示消息条数的小红点可以响应动作事件,也有人问我这样的动画该怎么做,这里就把实现的思路简单的描述一下.在实现的过程中,同样发现该功能并没有看到的那么简单,要做一个完备 ...

  5. js cookie使用

    if (window.localStorage) { window.localStorage.setItem('cname', "cvalue"); }else{ setCooki ...

  6. A - Chess Placing CodeForces - 985A

    You are given a chessboard of size 1 × n. It is guaranteed that n is even. The chessboard is painted ...

  7. iOS 越狱后 SSH 不能连接

    10.3 越狱之后,装上 OpenSSH, 但是发现连接不上,一直卡着不动,端口是通的,但是就是一直卡着.解决方法是在 Cydia 添加源 http://apt.cydiaba.cn,搜索 Dropb ...

  8. 可编辑div中包含子元素时获取光标位置不准确的问题

    前言: 高亮显示输入框中的关键字符,这就必须得用到可编辑div(或其他标签)元素了,这时我们需要获取光标的位置,以便插入字符. 正文: 正常情况下获取光标位置,代码如下: function getPo ...

  9. Python爬虫——Scrapy整合Selenium案例分析(BOSS直聘)

    概述 本文主要介绍scrapy架构图.组建.工作流程,以及结合selenium boss直聘爬虫案例分析 架构图 组件 Scrapy 引擎(Engine) 引擎负责控制数据流在系统中所有组件中流动,并 ...

  10. linux-2.6.22.6内核启动分析之配置

    配置过程最终结果是生成.config文件,我们想要对配置的目的有很清楚的了解,必须先对.config文件进行分析.通过cd命令切换到linux-2.6.22.6内核目录,输入vi .config 可以 ...