【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 ...
随机推荐
- Android SimpleAdapter的参数
1.作用是ArrayList和 ListView的桥梁.这个ArrayList里边的每一项都是一个Map<String,?>类型. ArrayList当中的每一项 Map对象都 ...
- Android批量打包提速 - 1分钟900个市场不是梦
版权声明: 欢迎转载,但请保留文章原始出处 作者:GavinCT 出处:http://www.cnblogs.com/ct2011/p/4152323.html 黎明前的黑暗 使用Ant或者Gradl ...
- leetCode题解之First Missing Positive
1.问题描述 2.题解思路 本题的思路是对于数组中每个正的元素,应该将其放到数组中对应的位置,比如元素1 ,应该放在数组的第一个位置.以此类推,最后检查数组中元素值和下标不匹配的情况. 3.代码 in ...
- spring boot(15)-异常处理
异常传递 如图:服务层和dao层的异常最终都会到达控制层,控制层的异常则会自动记入logback日志系统.所以我们应该在控制层来捕获系统异常 捕获控制层异常 import org.slf4j.Logg ...
- inline-flex值的含义
css中, flex设置在container中,默认情况下占用全部空间,但是如果使用inline-flex值,则container具有inline-block元素的某种特点,只占用需要的宽度
- 使用 Jenkins 和 Team Services 将应用部署到 Linux VM
持续集成 (CI) 和持续部署 (CD) 是一个管道,可以通过它生成.发布和部署代码. Team Services 针对到 Azure 的部署提供了一组完整的功能完备的 CI/CD 自动化工具. Je ...
- 如何在ScrollView滑动的瞬间禁用拖拽手势
如何在ScrollView滑动的瞬间禁用拖拽手势 效果: 在UIScrollView滑动的瞬间禁用pan手势,可以防止用户按着屏幕不放后导致出现的一些莫须有的bug. // // ViewContro ...
- Exchange & Office 365最小混合部署
前言 这篇文章的主题是混合部署~ 混合使得本地组织和云环境像一个单一的.协作紧密的组织一样运作.当组织决定进行混合部署,达到本地Exchange Server和Office 365共存的状态时,就会面 ...
- (matlab)plot画图的颜色线型(转)
http://wenku.baidu.com/link?url=SVVMVH8QlDIu2hVKDtoBYs6l0CnQvFnFHJJ9yexmYVKQqhz47qIr7aK7LOf8nN0qNdy8 ...
- 微信小程序 取随机数
第一思路 如:现在要从50< . =<100内取一个随机数: ran.Next(1,50)生成的是1-50的随机数+50 结束 第二思路 Math.random()的取值范围是: 0&l ...