二进制加法 https://discuss.leetcode.com/topic/33693/another-simple-java

    public String addBinary(String a, String b) {
if(a==null||b==null){
return a==null?b:a; //如果其中一个为null,则返回另一个;结合着if条件语句可以包括两个都是null的情形
}
int alen = a.length()-1;
int blen = b.length()-1;
StringBuffer sb = new StringBuffer();
int carry=0;
for(;alen>=0||blen>=0||carry>0;alen--,blen--){ //终止条件:指针a,指针b都遍历完了,并且carry为0
int sum=0; //这里的求和是对字符加减,所以采用了当前字符减去‘0‘字符就可以得到数值
sum+= (alen>=0)?a.charAt(alen)-'0':0; //先加上a的值
sum+=(blen>=0)?b.charAt(blen)-'0':0; //再加上b的值
sum+=carry;
carry = sum/2;
sum = sum%2;
sb.append(sum);
}
// if(carry>0)
// sb.append(carry);
return sb.reverse().toString();
}

Leetcode: 67. Add Binary的更多相关文章

  1. 【LeetCode】67. Add Binary

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

  2. 【LeetCode】67. Add Binary 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BigInteger类 模拟加法 日期 题目地址:h ...

  3. 【一天一道LeetCode】#67. Add Binary

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...

  4. [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings

    这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...

  5. 67. Add Binary【LeetCode】

    67. Add Binary Given two binary strings, return their sum (also a binary string). For example,a = &q ...

  6. LeetCode练题——67. Add Binary

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

  7. Java [Leetcode 67]Add Binary

    题目描述: Given two binary strings, return their sum (also a binary string). For example,a = "11&qu ...

  8. LeetCode 67. Add Binary (二进制相加)

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

  9. [LeetCode] 67. Add Binary 二进制数相加

    Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...

随机推荐

  1. 20145229吴姗珊《JAVA程序设计》第一周学习总结

    教材学习内容总结 第一章 JAVA 平台概论 1.JAVA不仅仅是一门程序设计语言,还是标准规范 2.1995年5月23日被公认为JAVA的诞生日 3.J2SE包含了JDK和JAVA程序语言 4.三大 ...

  2. hd acm2025

    问题:平面上有n条折线,问这些折线最多能将平面分割成多少块? 思路:像这种平面被线段分割成几部分的问题,80%用递推解决,因为n条线段与(n-1)条线段能建立联系.   你可以作图观察一下,会发现新增 ...

  3. Linux电源管理(3)-Generic PM之reboot过程【转】

    本文转载自:http://www.wowotech.net/pm_subsystem/reboot.html 1. 前言 在使用计算机的过程中,关机和重启是最先学会的两个操作.同样,这两个操作在Lin ...

  4. EntityFramework 学习 一 Spatial Data type support in Entity Framework 5.0

    MS SQl Server引进两种特殊的数据类型geography and geometry public partial class Course { public Course() { this. ...

  5. JavaScriptr -- 常用对象 String, date, prototype

    <script type="text/javascript"> //给已有的对象添加自定义功能 function getMax() { var max = this[0 ...

  6. Html 表单表格 form table

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. UOJ279 【UTR #2】题目交流通道

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000  作者博客:http://www.cnblogs.com/ljh2000-jump ...

  8. 囤题&&发布记录

    声明 && 温馨提示 by ljh2000 听说有人很喜欢狙我......看我不把你们抓起来嘿嘿嘿! 为了采取措施,不让被狙成为生活常态(雾   ,我要闭关锁国辣,我要开始(屯田)囤题 ...

  9. leetcode 24. Swap Nodes in Pairs(链表)

    Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...

  10. Activity间数据传输

    当对Android有一些了解后,不难发现,Android程序UI框架接近于Web页面的概念.每一个用于呈现页面的组件,Activity,都是彼此独立的,它们通过系统核心来调度整合,彼此之间的通过Int ...