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 ...
随机推荐
- python面向对象魔术方法补充
一.描述符 在 面向对象 编程中 定义一个(没有定义方法)类:class person , 在这个类里面,有name,age, heigth, weight,等等属性, 这个类就可以看作一个对 per ...
- Wannafly挑战赛24游记
Wannafly挑战赛24游记 A - 石子游戏 题目大意: A和B两人玩游戏,总共有\(n(n\le10^4)\)堆石子,轮流进行一些操作,不能进行下去的人则输掉这局游戏.操作包含以下两种: 把石子 ...
- Kernel 4.9的BBR拥塞控制算法与锐速
重要的事情说三遍! BBR并不能突破带宽限制!!! BBR并不能突破带宽限制!!! BBR并不能突破带宽限制!!! 它的功能如下: 1.在高丢包率与低速率的网络中提升传输效果,充分利用带宽. 2.降低 ...
- Dell PowerEdge R710服务器内存条插法/Dell 11G/12G系列服务器内存条插法(转)
说明:以我的经验,其实插3/6/9这个顺序去一定没有错. DELL PowerEdge R710服务器支持 DDR3的 DIMM (RDIMM) 或 ECC非缓冲的 DIMM(UDIMM).单列和双列 ...
- LinkedIn实时低延迟数据抓取系统Databus开源
http://www.infoq.com/cn/news/2013/03/linkedin-databus
- Programming 2D Games 读书笔记(第六章)
http://www.programming2dgames.com/chapter6.htm 示例一:Bounce 边界碰撞测试 velocity为移动的速度, 超过右边界,velocity.x为 ...
- [Winform]Cefsharp重写alert与confirm弹窗
摘要 在使用winform内嵌cefsharp浏览本地页面的时候,如果出现alert弹窗,会在标题栏显示页面所在目录.所以想起来重写alert的样式,通过winform的MessageBox进行提示. ...
- Unity3D实践系列07,组件的启用或禁用开关,物体的的可见或不可见开关,以及相应事件
创建一个Unity项目. 在"Project"窗口中,在"Asserts"中,添加"_MyScene"文件夹. 点击"File&q ...
- 通过进程ID获取基地址
下面代码是通过进程ID来获取进程的基地址,创建一个进程快照后,读取进程模块,一般情况下第一个模块就是进程的基地址,下面的程序通过模块的字符串匹配来找到基地址.通过MODULEENTRY32来读取,下面 ...
- VirtualBox - NAT虚拟机访问外网 + Host-Only物理主机虚拟机互访
[root@localhost ~]# vi /etc/sysconfig/network-scripts/ifcfg-System_eth0 # 未手动设定HOST-ONLY静态IP时的默认值 #T ...