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

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


题解:简单的二进制加法模拟。a,b的最后以为对齐开始进行加法,用carries保存进位,如果加完后最高位还有进位,那么要在结果的最前面加一个1。

代码如下:

 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; String s = new String();
int carries = 0;
int a_kepeler = a.length()-1;
int b_kepeler = b.length()-1; while(a_kepeler >= 0 && b_kepeler >= 0){
int sum = carries + a.charAt(a_kepeler) - '0' + b.charAt(b_kepeler) - '0';
carries = sum / 2;
sum = sum % 2;
s = String.valueOf(sum) + s;
a_kepeler--;
b_kepeler--;
} while(a_kepeler >= 0){
int sum = carries + a.charAt(a_kepeler) - '0';
carries = sum / 2;
sum = sum % 2;
s = String.valueOf(sum) + s;
a_kepeler--;
} while(b_kepeler >= 0){
int sum = carries + b.charAt(b_kepeler) - '0';
carries = sum / 2;
sum = sum % 2;
s = String.valueOf(sum) + s;
b_kepeler--;
} if(carries > 0)
s = "1" + s; return s;
}
}

上述代码还可以优化,就是如果a的长度小于b,就把a,b交换,使得a总是较长的那个,那么就可以省略第30~36行的while循环了。

【leetcode刷题笔记】Add Binary的更多相关文章

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

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

  2. LeetCode刷题笔录Add Binary

    Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...

  3. 【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, ...

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

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

  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刷题笔记和想法(C++)

    主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...

  7. 18.9.10 LeetCode刷题笔记

    本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...

  8. LeetCode刷题笔记 - 12. 整数转罗马数字

    学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...

  9. LeetCode练题——67. Add Binary

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

  10. Leetcode刷题笔记(双指针)

    1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...

随机推荐

  1. Android下Fragment使用(全集)

    1 http://blog.csdn.net/niu_gao/article/details/7163263  思路清晰,讲解详细,代码偏少,推荐高手阅读 2 http://blog.csdn.net ...

  2. go学习资料

    go书单 1.代码规范 https://github.com/golang/go/wiki/CodeReviewComments 2.基础知识 先看: https://github.com/mikel ...

  3. Windows安装Redis的php扩展

    Redis是一种常用的非关系型数据库,主要用作数据缓存,数据保存形式为key-value,键值相互映射.它的数据存储跟MySQL不同,它数据存储在内存之中,所以数据读取相对而言很快,用来做高并发非常不 ...

  4. Java和C++ 比較

    总体差别 1. C/C++是直接执行在机器上(编译后为机器码),而java编译后产生*.class文件(字节码)是执行在java虚拟机上在(JVM),经过JVM解译(机器码)再放到真实机器上执行. J ...

  5. Active Directory的基本概念

    前言 本文是面对准备加入Active Directory编程的初学者的一份文章,主要是讲解Active Directory(活动目录)的一些概念和相关知识.这篇文章本来是不想写下来的,因为概念性内容的 ...

  6. Nginx 配置指令的执行顺序

    在一个 location 中使用 content 阶段指令时,通常情况下就是对应的 Nginx 模块注册该 location 中的“内容处理程序”.那么当一个 location 中未使用任何 cont ...

  7. 工具类之Condition

    再次看到Condition,第一感觉还是觉得它和Mutex的功能是一样的,没必要存在.心里这么想,其实自己也知道怎么可能多余呢?老老实实的再分析一下代码,这次一定要把理解出来的内容记下来!都怪平时写代 ...

  8. dynamic_cast, RTTI, 整理

    主要是参考下图,了解内存布局,然后写个实例程序就差不多明白了,但是需要熟悉指针转换. 1) 只有多态类才有RTTI信息,dynamic_cast正是运用RTTI进行转换,属于运行时类型检查. 2) d ...

  9. saltstack之软件管理

    1.installed安装软件包 例: 安装NFS /srv/salt/pkg/nfs.sls nfs: pkg.installed: - pkgs: - nfs-utils 在命令行执行如下 sal ...

  10. C语言基础知识【判断】

    C 判断1.判断结构要求程序员指定一个或多个要评估或测试的条件,以及条件为真时要执行的语句(必需的)和条件为假时要执行的语句(可选的).C 语言把任何非零和非空的值假定为 true,把零或 null ...