Add Binary Leetcode java
题目:
Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100"
.
题解:
二进制加法都是从最低位(从右加到左)。所以对两个字符串要从最后一位开始加,如果遇见长度不一的情况,就把短的字符串高位补0.
每轮计算要加上进位,最后跳出循环后要坚持进位是否为1,以便更新结果。
代码如下(from discussion):
- 1 public String addBinary(String a, String b) {
- 2 int m = a.length();
- 3 int n = b.length();
- 4 int carry = 0;
- 5 String res = "";
- 6 // the final length of the result depends on the bigger length between a and b,
- 7 // (also the value of carry, if carry = 1, add "1" at the head of result, otherwise)
- 8 int maxLen = Math.max(m, n);
- 9 for (int i = 0; i < maxLen; i++) {
- // start from last char of a and b
- // notice that left side is int and right side is char
- // so we need to minus the decimal value of '0'
- int p=0,q=0;
- if(i<m)
- p = a.charAt(m-1-i) - '0';
- else
- p = 0;
- if(i<n)
- q = b.charAt(n-1-i)-'0';
- else
- q = 0;
- int tmp = p + q + carry;
- carry = tmp / 2;
- res += tmp % 2;
- }
- return (carry == 0) ? res : "1" + res;
- }
Add Binary Leetcode java的更多相关文章
- LeetCode算法题-Add Binary(Java实现)
这是悦乐书的第157次更新,第159篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第16题(顺位题号是67).给定两个二进制字符串,返回它们的总和(也是二进制字符串).输 ...
- Add Binary <leetcode>
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...
- LeetCode 面试:Add Binary
1 题目 Given two binary strings, return their sum (also a binary string). For example,a = "11&quo ...
- # Leetcode 67:Add Binary(二进制求和)
Leetcode 67:Add Binary(二进制求和) (python.java) Given two binary strings, return their sum (also a binar ...
- leetcode解题:Add binary问题
顺便把之前做过的一个简单难度的题也贴上来吧 67. Add Binary Given two binary strings, return their sum (also a binary strin ...
- [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings
这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...
- LeetCode: Add Binary 解题报告
Add BinaryGiven two binary strings, return their sum (also a binary string). For example,a = "1 ...
- 67. Add Binary【LeetCode】
67. Add Binary Given two binary strings, return their sum (also a binary string). For example,a = &q ...
- leetcode笔记:Add Binary
一.题目描写叙述 Given two binary strings, return their sum (also a binary string). For example, a = "1 ...
随机推荐
- BZOJ2190 [SDOI2008]仪仗队 [欧拉函数]
题目描述 作为体育委员,C君负责这次运动会仪仗队的训练.仪仗队是由学生组成的N * N的方阵,为了保证队伍在行进中整齐划一,C君会跟在仪仗队的左后方,根据其视线所及的学生人数来判断队伍是否整齐(如下图 ...
- BZOJ1170 : [Balkan2007]Cipher
首先对于每个位置,求出它开始长度为y的横行的hash值,然后对于hash值再求一次竖列的hash值,排序后求出众数即可. 时间复杂度$O(n^2\log n)$. #include<cstdio ...
- GitLab查询当前版本
gitlab-rake gitlab:env:info 其实还有很多方法可以参考GitLab的帮助文档:https://docs.gitlab.com/omnibus/README.html 参考: ...
- CentOS 7搭建OpenVPN-Admin
安装注意要点: 1.用户及目录权限 2.openvpn配置文件/etc/openvpn/server.conf,可以设置不同的转发模式等等 3.全程使用apache,不要用其它的如nginx这些,不然 ...
- J1850 Implement
http://avrobdii.googlecode.com/svn/trunk/code/J1850.c /* Copyright (C) Trampas Stern name of author ...
- TeeChart 有用的属性
//背景 BackWall.Gradient.Visible = True //是否显示右边图标选项 Legend.Visible = False //不在显示3D效果, 比较有用 View3D = ...
- SQL Server 2000 绿色精简版gsql适用于xp/win7/win8/win10
老的程序员肯定都用过sql2000数据库,我在2006-2010年之间,做的不少网站也都是sql2000数据库的,但是后来随着mysql的兴起,就逐渐不再使用sql数据库了.但是最近有个客户的网站要修 ...
- 有关AngularJS请求Web API资源的思路
页面部分大致如下: <body ng-app="productManagement"> ... <div ng-include="'app/produc ...
- 在ASP.NET MVC中使用Knockout实践09,自定义绑定
Knockout真正强大之处在于绑定机制,通过data-bind属性值体现绑定,不仅可以绑定值,还可以绑定事件,甚至可以自定义绑定. 从一个例子看Knockou的绑定机制 假设想给一个button元素 ...
- future封装了callable,thread封装future。
三.使用Callable,Future返回结果 总结:future封装了callable,thread封装future.将callable的返回结果封装在future中,thread封装future, ...