题目描述:

解题思路:

  此题的思路简单,下面的代码用StringBuilder更加简单,注意最后的结果要反转过来。【LeetCode415】Add Strings的解法和本题一模一样。

java代码:

  1. public class LeetCode67 {
  2. public static void main(String[] args) {
  3. String a="11",b="1";
  4. System.out.println(a+"和"+b+"相加的结果是:"+new Solution().addBinary(a, b));
  5. }
  6. }
  7. class Solution {
  8. public String addBinary(String a, String b) {
  9. StringBuilder sb=new StringBuilder();
  10. int i=a.length()-1,j=b.length()-1;
  11. int carry=0;
  12. while(i>=0||j>=0){
  13. int sum=carry;
  14. if(i>=0) sum+=a.charAt(i--)-'0';
  15. if(j>=0) sum+=b.charAt(j--)-'0';
  16.  
  17. sb.append(sum%2);
  18. carry=sum/2;
  19. }
  20. if(carry!=0) sb.append(carry);
  21. return sb.reverse().toString();
  22. }
  23. }

程序结果:

【LeetCode67】 Add Binary的更多相关文章

  1. 【leetcode】Add Binary

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

  2. 【Leetcode】【Easy】Add Binary

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

  3. 【LeetCode415】Add Strings

    题目描述: 解决思路: 此题较简单,和前面[LeetCode67]方法一样. Java代码: public class LeetCode415 { public static void main(St ...

  4. 【LeetCode445】 Add Two Numbers II★★

    题目描述: 解题思路: 给定两个链表(代表两个非负数),数字的各位以正序存储,将两个代表数字的链表想加获得一个新的链表(代表两数之和). 如(7->2->4->3)(7243) + ...

  5. 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)

    [LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...

  6. 【LeetCode】145. Binary Tree Postorder Traversal

    Difficulty: Hard  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/binary-tree-pos ...

  7. 【LeetCode】Validate Binary Search Tree ——合法二叉树

    [题目] Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defin ...

  8. 【LeetCode】Balanced Binary Tree 解题报告

    [题目] Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bi ...

  9. 【LeetCode】二叉查找树 binary search tree(共14题)

    链接:https://leetcode.com/tag/binary-search-tree/ [220]Contains Duplicate III (2019年4月20日) (好题) Given ...

随机推荐

  1. 分享到xxx

    来源百度 一.概述 百度分享代码已升级到2.0,本页将介绍新版百度分享的安装配置方法,请点击左侧列表查看相关章节. 二.代码结构 分享代码可以分为三个部分:HTML.设置和js加载,示例如下: 代码结 ...

  2. 移动端meta标签设置

    移动端meta标签设置 1.设置当前html文件的字符编码 <meta charset="UTF-8"> 1 2设置浏览器的兼容模式(让IE使用最新的浏览器渲染) &l ...

  3. vip会员统计表 (vip等级是灵活配置的 非写死1是金卡用户 2是什么 等)

      一个非常常见的报表,分析会员组成比例 以及最新增长情况 和上月同期会员增长情况. 比较特殊一点的是 报表中的 普通会员 和 金卡会员 临时会员 银卡会员 等列 都是根据会员等级配置表动态生成的(即 ...

  4. ArrayMap代替HashMap

    ArrayMap是一个<key,value>映射的数据结构,它设计上更多的是考虑内存的优化,内部是使用两个数组进行数据存储,一个数组记录key的hash值,另外一个数组记录Value值,它 ...

  5. linux 网络命令last、lastlog、traceroute、netstat

    last /usr/bin/last语法:last功能:列出目前与过去登入系统的用户信息 reboot 是重启信息 lastlog lastlog -u 502(用户ID) traceroute /b ...

  6. 转:.net设计模式之工厂模式

    原文:http://terrylee.cnblogs.com/archive/2006/01/04/310716.html 概述 在软件系统中,经常面临着“某个对象”的创建工作,由于需求的变化,这个对 ...

  7. [UI] Pull menu interaction concept - 下拉菜单交互

    Pull menu interaction concept - 下拉菜单交互 http://freebiesbug.com/code-stuff/pull-menu-interaction-conce ...

  8. MDT概念说明

    转自:http://www.winos.cn/html/21/t-39621.html         http://hi.baidu.com/popweb/item/95ea6cf3aea966b5 ...

  9. Redis学习---Redis操作之String

    set(name, value, ex=None, px=None, nx=False, xx=False) 在Redis中设置值,默认,不存在则创建,存在则修改 参数:      ex,过期时间(秒 ...

  10. 铁乐学Python_Day33_网络编程Socket模块1

    铁乐学Python_Day33_网络编程Socket模块1 部份内容摘自授课老师的博客http://www.cnblogs.com/Eva-J/ 理解socket Socket是应用层与TCP/IP协 ...