Lintcode二进制求和
来源:互联网 发布:文言虚词乎的用法 编辑:程序博客网 时间:2024/05/20 13:06
二进制求和
给定两个二进制字符串,返回他们的和(用二进制表示)。
a = 11
b = 1
返回 100
java中substring的用法
str=str.substring(int beginIndex);截取掉str从首字母起长度为beginIndex的字符串,将剩余字符串赋值给str;
str=str.substring(int beginIndex,int endIndex);截取str中从beginIndex开始至endIndex结束时的字符串,并将其赋值给str;
public class Solution {
/*
* @param a: a number
* @param b: a number
* @return: the result
*/
public String addBinary(String a, String b) {
// write your code here
int alen=a.length()-1;
int blen=b.length()-1;
String result="";
int sum=0;
while(alen>=0||blen>=0){
if(alen>=0){
sum+=Integer.parseInt(a.substring(alen,alen+1));
alen--;
}
if(blen>=0){
sum+=Integer.parseInt(b.substring(blen,blen+1));
blen--;
}
if(sum==2){
result="0"+result;
sum=1;
}
else if(sum==1||sum==0){
result=sum+""+result;
sum=0;
}
else if(sum==3){
result="1"+result;
sum=1;
}
}
if(sum==1){
result="1"+result;
}
return result;
}
}
- LintCode-二进制求和
- LIntCode-二进制求和
- 二进制求和(LintCode)
- lintcode ----二进制求和
- LintCode之二进制求和
- Lintcode 408 二进制求和
- LintCode【容易】二进制求和
- 二进制求和,lintcode
- LintCode | 408. 二进制求和
- LintCode 二进制求和
- lintcode --二进制求和
- Lintcode二进制求和
- 二进制求和-LintCode
- LintCode Add Binary 二进制求和
- lintcode add-binary 二进制求和
- LintCode(容易)二进制求和
- lintCode(408)——二进制求和
- LintCode(容易)二进制求和遇到的bug
- 设计模式之代理模式
- linux虚拟机上中挂载光驱报错 mount:youmustspecifythefilesystemtype
- 进程与线程的区别与联系
- java反射机制
- C#笔记(8)使应用具有管理员权限
- Lintcode二进制求和
- Python爬虫小记(一)
- 高精度非负数模板
- Java Web 会话保留技术
- Cordova 学习笔记一
- Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)
- c#从控制台输入问题
- request response session
- Ajax的ajax()和post()的使用