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

For example,

a = "11"

b = "1"

Return "100".

详细一位一位地加即可了,考虑进位的问题。还有最后记得把生成的string反过来再返回,由于我们是从最低位開始加的。

public class Solution {
public String addBinary(String a, String b) {
if(a == null || a.length() == 0)
return b;
if(b == null || b.length() == 0)
return a; StringBuilder res = new StringBuilder(); int i = a.length() - 1;
int j = b.length() - 1;
int digit;
int carry = 0; while(i >= 0 && j >= 0){
digit = (int)(a.charAt(i) - '0' + b.charAt(j) - '0' + carry);
carry = digit / 2;
digit %= 2; res.append(digit);
i--;
j--;
} while(i >= 0){
digit = (int)(a.charAt(i) - '0' + carry);
carry = digit / 2;
digit %= 2; res.append(digit);
i--;
} while(j >= 0){
digit = (int)(b.charAt(j) - '0' + carry);
carry = digit / 2;
digit %= 2; res.append(digit);
j--;
}
//don't forget to add the final carry(if exists)
if(carry > 0){
res.append(carry);
} return res.reverse().toString();
}
}

LeetCode刷题笔录Add Binary的更多相关文章

  1. LeetCode练题——67. Add Binary

    1.题目 67. Add Binary——easy Given two binary strings, return their sum (also a binary string). The inp ...

  2. 【leetcode刷题笔记】Binary Tree Level Order Traversal(JAVA)

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  3. 【leetcode刷题笔记】Binary Tree Inorder Traversal

    Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...

  4. 【leetcode刷题笔记】Binary Tree Preorder Traversal

    Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...

  5. 【leetcode刷题笔记】Binary Tree Level Order Traversal II

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  6. LeetCode刷题系列——Add Two Numbers

    题目链接 这个题目很简单,归并而已,好久没练编程,居然忘了在使用自定义类型前,要进行初始化(new操作). class ListNode{ int val; ListNode next; ListNo ...

  7. LeetCode刷题指南(字符串)

    作者:CYC2018 文章链接:https://github.com/CyC2018/CS-Notes/blob/master/docs/notes/Leetcode+%E9%A2%98%E8%A7% ...

  8. C#LeetCode刷题-字符串

    字符串篇 # 题名 刷题 通过率 难度 3 无重复字符的最长子串   24.6% 中等 5 最长回文子串   22.4% 中等 6 Z字形变换   35.8% 中等 8 字符串转整数 (atoi)   ...

  9. C#LeetCode刷题-数学

    数学篇 # 题名 刷题 通过率 难度 2 两数相加   29.0% 中等 7 反转整数 C#LeetCode刷题之#7-反转整数(Reverse Integer) 28.6% 简单 8 字符串转整数 ...

随机推荐

  1. Quartz1.8.5例子(十一)

    /* * Copyright 2005 - 2009 Terracotta, Inc. * * Licensed under the Apache License, Version 2.0 (the ...

  2. java 异常小结

    异常大体分为编译异常和运行异常两类,如果用软件开发(如Eclipse)编译异常在写代码时得到提醒, 而运行异常需要在运行时才能得到提示. 算术异常类:ArithmeticExecption 这个异常是 ...

  3. spring mvc 配置

    之前配置spring mvc 怎么都访不到对应的jsp,后来把prefix里面的jsp改为views,就能访问到了,然后再改回jsp也可以访问到 搞了两天,都崩溃了,不管怎样先把没问题的例子给记录下来 ...

  4. bzoj 2693: jzptab 线性筛积性函数

    2693: jzptab Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 444  Solved: 174[Submit][Status][Discus ...

  5. uva 12124 - Assemble

    最大值最小的题: 直接用二分,比较简单: 不过我的二分老是不用好.有时间总结一下! #include<cstdio> #include<map> #include<vec ...

  6. UVA 11426 GCD Extrme (Ⅲ)

    给定一个整数N(1<N<=4000000)的整数求∑GCD(i,j)i=1,2,3....j-1,2<=j<=n的值.参考了一下网上的题解,复述一下我理解后的思路,加深理解: ...

  7. RTSP

    相关博客: RTSP 很详细的英文文档 RTSP交互命令简介及过程参数描述   RTSP协议 http://blog.csdn.net/andyweike/article/details/621071 ...

  8. Qt 子窗口内嵌到父窗口中(无边框附体show即可)good

    有时需要把一个子窗口内嵌进入父窗口当中. 我们可以这样做 1.新建一个QWidget 或者QDialog的子类 ClassA(父类为ClassB) 2.在新建类的构造函数中添加设置窗口属性 setWi ...

  9. Java NIO原理及实例

    Java NIO是在jdk1.4开始使用的,它既可以说成“新I/O”,也可以说成非阻塞式I/O.下面是java NIO的工作原理: 1. 由一个专门的线程来处理所有的 IO 事件,并负责分发. 2. ...

  10. Hive JSON数据处理的一点探索

    背景   JSON是一种轻量级的数据格式,结构灵活,支持嵌套,非常易于人的阅读和编写,而且主流的编程语言都提供相应的框架或类库支持与JSON数据的交互,因此大量的系统使用JSON作为日志存储格式.   ...