题目描述:

解题思路:

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

java代码:

 public class LeetCode67 {
public static void main(String[] args) {
String a="11",b="1";
System.out.println(a+"和"+b+"相加的结果是:"+new Solution().addBinary(a, b));
}
}
class Solution {
public String addBinary(String a, String b) {
StringBuilder sb=new StringBuilder();
int i=a.length()-1,j=b.length()-1;
int carry=0;
while(i>=0||j>=0){
int sum=carry;
if(i>=0) sum+=a.charAt(i--)-'0';
if(j>=0) sum+=b.charAt(j--)-'0'; sb.append(sum%2);
carry=sum/2;
}
if(carry!=0) sb.append(carry);
return sb.reverse().toString();
}
}

程序结果:

【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. vue router 配合transition 切换动画

    把<router-view>嵌套在<transition>里,路由变化的时候,vue会为包裹页面的div增加动画样式,我们要做的就是监听路由变化.定义这些动画样式,以规定页面到 ...

  2. jquery刷新页面的实现代码(局部及全页面刷新)

    局部刷新: 这个方法就多了去了,常见的有以下几种: $.get方法,$.post方法,$.getJson方法,$.ajax方法如下 前两种使用方法基本上一样 下面介绍全页面刷新方法:有时候可能会用到  ...

  3. oracle截取字符串去掉字段末尾指定长度的字符

    lengthb(string)计算string所占的字节长度:返回字符串的长度,单位是字节 length(string)计算string所占的字符长度:返回字符串的长度,单位是字符 eg: //去掉该 ...

  4. 32位Windows7 利用多余的不能识别的电脑内存 RAMDISK5.5教程

    32位Windows7 利用多余的不能识别的电脑内存 RAMDISK5.5教程 环境:Windows7 32位 Ultimate 内存8GB 只能识别2.95GB内存 ramdisk5.5只适用于Wi ...

  5. ExpressRoute 路由要求

    若要使用 ExpressRoute 连接到 Azure 云服务,需要设置并管理路由.某些连接服务提供商以托管服务形式提供路由的设置和管理.请咨询连接服务提供商,以确定他们是否提供此类服务.如果不提供, ...

  6. 转:eclipse的快捷键

    22 21 Eclipse中10个最有用的快捷键组合  一个Eclipse骨灰级开发者总结了他认为最有用但又不太为人所知的快捷键组合.通过这些组合可以更加容易的浏览源代码,使得整体的开发效率和质量得到 ...

  7. linux something

    使用的UbuntuKylin  source error e1:apt-get update提示没有证书 e2:访问不了阿里云源服务器 e3:GPG 错误:http://download.mono-p ...

  8. [翻译] popping

    https://github.com/schneiderandre/popping Popping is a collection of animation examples for iOS apps ...

  9. [翻译] WPAttributedMarkup

    WPAttributedMarkup https://github.com/nigelgrange/WPAttributedMarkup WPAttributedMarkup is a simple ...

  10. NCE3

    Lesson1  A puma at large Pumas are large, cat-like animals which are found in America. When reports ...