【LeetCode67】 Add Binary
题目描述:

解题思路:
此题的思路简单,下面的代码用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的更多相关文章
- 【leetcode】Add Binary
题目简述: Given two binary strings, return their sum (also a binary string). For example, a = "11&q ...
- 【Leetcode】【Easy】Add Binary
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...
- 【LeetCode415】Add Strings
题目描述: 解决思路: 此题较简单,和前面[LeetCode67]方法一样. Java代码: public class LeetCode415 { public static void main(St ...
- 【LeetCode445】 Add Two Numbers II★★
题目描述: 解题思路: 给定两个链表(代表两个非负数),数字的各位以正序存储,将两个代表数字的链表想加获得一个新的链表(代表两数之和). 如(7->2->4->3)(7243) + ...
- 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)
[LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...
- 【LeetCode】145. Binary Tree Postorder Traversal
Difficulty: Hard More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/binary-tree-pos ...
- 【LeetCode】Validate Binary Search Tree ——合法二叉树
[题目] Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defin ...
- 【LeetCode】Balanced Binary Tree 解题报告
[题目] Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bi ...
- 【LeetCode】二叉查找树 binary search tree(共14题)
链接:https://leetcode.com/tag/binary-search-tree/ [220]Contains Duplicate III (2019年4月20日) (好题) Given ...
随机推荐
- 用jquery实现带左右按键的轮播图
成品如下: 简单来说就是点击“右”按钮时,转换到右边的下一幅图片,同时上面的小方块颜色也跟着改变,如果已经是最后一幅图片,再点击“右”,则转换到第一幅图片,是直接向左移找到第一幅图的,明天再做一下无缝 ...
- 利用:before和:after伪类制作类似微信对话框
今天学到了怎么做一个小三角形,进而结合其他属性把类似微信对话框的图形做出来了. 先做出如下形状: .arrow { width: 30px; height:30px; border-width:20p ...
- CSS之浮动布局(float,浮动原理,清除/闭合浮动方法)
css之浮动布局 本人博客:查看文章 1.什么是浮动:在我们布局的时用到的一种技术,能够方便我们进行布局,通过让元素浮动,我们可以使元素在水平上左右移动,再通过margin属性调整位置 2.浮动的 ...
- 快速 图片颜色转换迁移 Color Transfer Opencv + Python
Super fast color transfer between images About a month ago, I spent a morning down at the beach, w ...
- Keras GlobalAveragePooling2D 示例代码
GlobalAveragePooling2D层 keras.layers.pooling.GlobalAveragePooling2D(dim_ordering=‘default‘) 为空域信号施加全 ...
- linux centos7最小化安装NAT模式网络设置
1.网络连接设置为NAT模式2.开启CentOS7,以root登陆3.vi /etc/sysconfig/network-scripts/ifcfg-ensXXXX4.设置BOOTPROTO=dhcp ...
- sql 字符、数字类型自动转换及运算
本页面所有内容也可以在oracle 运行,只需要把int.float .decimal 改为 number类型即可 -- 字符串转数字 int 类型 drop table test;create ta ...
- Sink Prosessor - Flume的可靠性保证:故障转移、负载均衡
Flume的一些组件(如Spooling Directory Source.File Channel)能够保证agent挂掉后不丢失数据. 1.负载均衡 1)Load balancing Sink P ...
- oracle lz047中的REGEXP_LIKE(cust_first_name,'[[:digit:]]')) .
转自http://blog.csdn.net/dream19881003/article/details/6680982 今天在看OCP题库的时候有一道题是考字段约束的,意思是要在表CUSTOMERS ...
- 第一章 数据库和SQL
1-1 数据库是什么? 一.数据库的含义 数据库是将大量数据保存起来,通过计算机加工而成的可以进行高效访问的数据集合. 数据库DB 二.数据库管理系统 DBMS 用来管理数据库的计算机系统称为 ...