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 ...
随机推荐
- HTTP协议-缓存
HTTP 协议中,缓存更多关心的文档资源的再利用.其目的是减少数据传输,加快相应速度等等.而对于缓存采用的是什么方案,也就是存在内存中还是硬盘中之类的问题,就属于另外的内容了. 假设,我身在广东,但是 ...
- windows servier2008+virtualenv下部署Flask (IIS+wfastcgi)
由于业务只有一台windows server2008的服务器,一般的nginx+uwsgi的部署方式不行,以下记录部署过程,本文参考自:https://www.cnblogs.com/xiaolecn ...
- sublime text2 用ctags插件实现方法定位(转)
我们用sublime几乎都会首先安装这个插件,这个插件是管理插件的功能,先安装它,再安装其他插件就方便了. 点击sublime的菜单栏 view->show console :现在打开了控制台, ...
- 一种高效的序列化方式——MessagePack
最近在弄一些数据分析方面的内容,发现很多时候数据瓶颈在模块之间的数据序列化和反序列化上了,原来项目中用的是Json,找了一圈发现Json.net在Json序列化库中已经是性能的佼佼者了,便准备从序列化 ...
- Cocos2d-x 3.0游戏开发之虚拟机IOS环境:匹配才是好,莫要随便升级软件
尊重开发人员的劳动成果.转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/34110449 做为一个买不起MAC的Coder,仅 ...
- OpenOCD Debug Adapter Configuration
Correctly installing OpenOCD includes making your operating system give OpenOCD access to debug adap ...
- Oracle绝对值函数
1.绝对值:abs() select abs(-2) value from dual; 2.取整函数(大):ceil() select ceil(-2.001) value from dual;(-2 ...
- java从文件中读取数据然后插入到数据库表中
实习工作中,完成了领导交给的任务,将搜集到的数据插入到数据库中,代码片段如下: static Connection getConnection() throws SQLException, IOExc ...
- (ios7) 解决代码布局View, ios7 中 subView 高度增加StatusBar20dp的问题,保证Ios6,ios7代码一致
在ios7 布局中,Status Bar 和 ToolBar ,NavigateBar 等都包含在ViewControl的主View中. 这样原来ios6 的View布局 整体向上移动了20dp,下面 ...
- C#编程(二十四)----------修饰符
修饰符 修饰符即应用于类型或成员的关键字.修饰符可以指定方法的可见性,如public或private,还可以指定一项的本质,如刚发的vritual或abstract. 可见性的修饰符 修饰符 应用于 ...