67. Add Binary

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

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

 public class Solution {
public String addBinary(String a, String b) {
String res ="";
int l1=a.length()-1;
int l2=b.length()-1; int carry=0;
for(int i=l1,j=l2;i>=0||j>=0;i--,j--){
int sum=carry;
sum+=(i>=0)?(int)(a.charAt(i)-'0'):0;
sum+=(j>=0)?(int)(b.charAt(j)-'0'):0;
res =sum%2+res;
carry=sum/2; }
if(carry!=0){
res=carry+res;
}
return res;
}
}

67. Add Binary【LeetCode】的更多相关文章

  1. LeetCode 67. Add Binary【个位补0,不必对齐】【easy】

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

  2. 【LeetCode】数学(共106题)

    [2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...

  3. 【LeetCode】字符串 string(共112题)

    [3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...

  4. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  5. 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)

    [LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...

  6. 【LeetCode】623. Add One Row to Tree 解题报告(Python)

    [LeetCode]623. Add One Row to Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problem ...

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

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

  8. 【leetcode】998. Maximum Binary Tree II

    题目如下: We are given the root node of a maximum tree: a tree where every node has a value greater than ...

  9. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告

    [LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...

随机推荐

  1. 基于 HTML5 WebGL 的 3D 网络拓扑图

    在数据量很大的2D 场景下,要找到具体的模型比较困难,并且只能显示出模型的的某一部分,显示也不够直观,这种时候能快速搭建出 3D 场景就有很大需求了.但是搭建 3D 应用场景又依赖于通过 3ds Ma ...

  2. RocksDB上锁机制

    RocksDB作为一个开源的存储引擎支持事务的ACID特性,而要支持ACID中的I(Isolation),并发控制这块是少不了的,本文主要讨论RocksDB的锁机制实现,细节会涉及到源码分析,希望通过 ...

  3. tomcat启动不了,内存溢出

    今天下午不知道做了什么,然后tomcat启动了10分钟还启动不了.然后看控制台报错信息,说是内存溢出.然后就各种百度,终于解决了.在这里记录提示自己,避免这种问题再次出现还要浪费时间去找方法解决. 最 ...

  4. VMware Mac OS中无法找到适应的分辨率的解决办法

    使用VMware安装的Mac OS中,有时在显示器的分辨率中的选择项里面,没有对应显示的分辨率可供选择的时候(无法自适应),可以在虚拟机设置里,显示器中修改强制分辨率.修改过后重启虚拟机,就可以有对应 ...

  5. C++ 宏定义#define 中##的使用

    在C++的宏定义中,符号##一般是用于连接,包括参数的连接,参数与标识符的连接等,然后形成一个新的标识符. 下面举几个例子来进行说明. eg1: #define ADD(a,b) a##b #defi ...

  6. js加强版图片轮播

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

  7. React Image加载图片过大导致ListView滑动卡顿

    今天莫名的发现ListView加载Item很卡,一顿一顿的... ListView Item 中只加载一张图片,小编从百度爸爸上随便复制的链接,这张图片很大,以致埋下如此大坑... Image的Sty ...

  8. 浙江省新高中信息技术教材,将围绕Python进行并增加编程相关知识点

    2017年初消息: 浙江省信息技术新教材,即将在2017级(2017年9月入学)高中新生中开始使用. 据了解,与目前的选考(可以理解为高考科目)要求的信息技术教材由3本<信息技术基础>.& ...

  9. tcp/ip通信传输流

    利用TCP/IP协议族进行网络通信时,会通过分层顺序与对方进行通信,发送端从应用层往下走,接收端则往应用层方向走. 我们用HTTP进行举例 客户端在应用层发出想要看到某个web页面的http请求.HT ...

  10. User implements HttpSessionBindingListener

    public class User implements HttpSessionBindingListener { private int id; private String userName; p ...