067 Add Binary 二进制求和
给定两个二进制字符串,返回他们的和(用二进制表示)。
案例:
a = "11"
b = "1"
返回 "100" 。
详见:https://leetcode.com/problems/add-binary/description/
Java实现:
class Solution {
public String addBinary(String a, String b) {
String s="";
int c=0;
int i=a.length()-1;
int j=b.length()-1;
while(i>=0||j>=0||c==1){
c+=i>=0?a.charAt(i)-'0':0;
c+=j>=0?b.charAt(j)-'0':0;
s=(char)(c%2+'0')+s;
c/=2;
--i;
--j;
}
return s;
}
}
067 Add Binary 二进制求和的更多相关文章
- # Leetcode 67:Add Binary(二进制求和)
Leetcode 67:Add Binary(二进制求和) (python.java) Given two binary strings, return their sum (also a binar ...
- lintcode:Add Binary 二进制求和
题目: 二进制求和 给定两个二进制字符串,返回他们的和(用二进制表示). 样例 a = 11 b = 1 返回 100 解题: 和求两个链表的和很类似 考虑进位,考虑最后一项的进位 0+0 = 0 不 ...
- Add Strings大整数加法十进制求和 & Add Binary二进制求和
[抄题]: 以字符串的形式给出两个非负整数 num1 和 num2,返回 num1和 num2 的和. 比如一个50位+一个100位. 给定 num1 = "123",num2 = ...
- leetCode 67.Add Binary (二进制加法) 解题思路和方法
Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...
- Java for LeetCode 067 Add Binary
Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...
- 【LeetCode每天一题】Add Binary(二进制加法)
Given two binary strings, return their sum (also a binary string).The input strings are both non-emp ...
- [leetcode]67. Add Binary 二进制加法
Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...
- [Leetcode] add binary 二进制加法
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 ...
随机推荐
- openfire插件(1)
插件核心类,这里对PacketInterceptor.Plugin进行继承.如果开发插件就一定要继承Plugin,而继承PacketInterceptor是拦截用户发送的消息包.对消息包进行过滤.拦截 ...
- DBSCAN 聚类分析
DBSCANCLUSTER DBSCAN(Density-basedspatial clustering ofapplications with noise)Martin.Ester, Hans-Pe ...
- phpstorm 代码按列对齐
设置方式: Preference... -> Editor -> CodeStyle -> PHP -> Other -> Align key-value pairs
- 1139 First Contact(30 分)
Unlike in nowadays, the way that boys and girls expressing their feelings of love was quite subtle i ...
- 基于bootsplash的嵌入式linux启动画面定制
来源: ChinaUnix博客 作者: ChinaUnix博客 发布时间:2007-01-01 16:29:00 摘 要:在基于linux的嵌入式仿真平台研发中,利用开源工具bootsplash能够定 ...
- BZOJ1500:[NOI2005]维修数列
浅谈\(splay\):https://www.cnblogs.com/AKMer/p/9979592.html 浅谈\(fhq\)_\(treap\):https://www.cnblogs.com ...
- Java中的内部类介绍(1)
栗子1: package campu; //外部类 class Out{ private int age =12; //内部类 class In{ public void print(){ Syste ...
- SQL 排序规则问题
http://blog.csdn.net/delphigbg/article/details/12744807 MSSQL排序规则总结 什么是排序规则呢? 排序规则根据特定语言和区域设置标准指定对 ...
- 连接Oracle数据库的Hibernate配置…
连接Oracle数据库的Hibernate配置文件 连接Oracle的Hibernate配置文件有两种格式,一种是xml格式的,另一种是Java属性文件格式的.下面分别给出这两种格式配置文件的代码. ...
- 利用CSS3给图片添加旋转背景特效
首先看旋转特效:http://***/demo/201512/2015-12-09-css3-image-hover-animate/index.html 这是一款纯CSS3实现的当鼠标滑过图片时文字 ...