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. 剑指offer 练习题目

    #include <iostream> #include<vector> #include <stack> #include<map> #include ...

  2. Tomcat常用配置修改

    Tomcat常用配置修改 说明 运行需要设置环境变量 JAVA_HOME 即JDK安装目录 tomcat 默认登录地址 http://localhost:8080 配置tomcat 1.端口设置 打开 ...

  3. .NET C#到Java没那么难,MVC篇

    最典型的JAVA MVC就是JSP + servlet + javabean的模式.比较好的MVC,老牌的有Struts.Webwork.新兴的MVC 框架有Spring MVC.Tapestry.J ...

  4. Python文件系统功能:os模块

    Python文件系统功能:os模块 1.os模块方法分类 (1)目录: chdir() 改变工作目录 chroot() 设定当前进程的根目录 listdir() 列出指定目录下的所有文件名 mkdir ...

  5. 读书笔记-Software Testing(By Ron Patton)

    Software Testing Part I:The Big Picture 1.Software Testing Background Bug's formal definition 1.The ...

  6. mybatis学习笔记(五) -- maven+spring+mybatis从零开始搭建整合详细过程(附demo和搭建过程遇到的问题解决方法)

    文章介绍结构一览 一.使用maven创建web项目 1.新建maven项目 2.修改jre版本 3.修改Project Facts,生成WebContent文件夾 4.将WebContent下的两个文 ...

  7. Linux配置虚拟地址

    立即生效: [root@server ~]$ 192.168.1.101 broadcast 192.168.1.255 netmask 255.255.255.0 up [root@server ~ ...

  8. MQ队列管理

    分享一段代码,很实用. 下面这段java代码是我在国外一个论坛上发现的,源地址已经忘了.代码的作用是可以删除正在使用的mq的队列消息,管理mq的人一定知道它的美妙了吧,哈哈. 我拿来改了下,增加了2个 ...

  9. powershell 统计AD中所有计算机及对应的操作系统信息

    要想用powershell管理域,首先先加载activedirectory模块 PS C:\> import-module activedirectory 下面就可以利用get-adcomput ...

  10. Java探秘之神秘的字符串String(二)

    不可变性 String可以说是最常用的类型了,即字符串类型,String是常量(final关键词修饰的),他的值不能被创建后更改,因为字符串是不可被改变的,所以可以被用来共享. Java语言为Stri ...