LeetCode OJ:Add Binary(二进制相加)
Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100"
.
简单的二进制相加而已,只不过传入的参数是字符串而已。为了方便,先将string reverse了一下,代码如下:
class Solution {
public:
string addBinary(string a, string b) {
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
int len1 = a.length();
int len2 = b.length();
string result;
int flag ,val;
flag = val = ;
int i;
for(i = ; i < len1 && i < len2; ++i){
val = (a[i] - '') + (b[i] - '') + flag;
result.append(, val % + '');
flag = val/;
}
while (i < len1) {
val = a[i] - ''+ flag;
result.append(, val % + '');
flag = val/;
++i;
}
while (i < len2 ){
val = b[i] - ''+ flag;
result.append(, val % + '');
flag = val/;
++i;
}
cout << "flag " << flag << endl;
if(flag != )
result.append(, '');
reverse(result.begin(), result.end());
return result;
}
};
java:以前的循环用的好蠢啊,居然用了三次循环,下面的java稍微有些改进:
public class Solution {
public String addBinary(String a, String b) {
int len1 = a.length();
int len2 = b.length();
String ret = new String();
int i = 0;
int carry = 0;
a = ReverseStr(a);
b = ReverseStr(b);
while(i < len1 || i < len2 || carry != 0){
int val = ((i<len1)?(a.charAt(i)-'0'):0) + ((i<len2)?(b.charAt(i)-'0'):0) + carry;
carry = 0;
if(val > 1){
carry = val/2;
ret += (char)((val%2) + '0');
}else{
ret += (char)(val + '0');
}
i++;
}
return ReverseStr(ret);
} public String ReverseStr(String str){
return new StringBuffer(str).reverse().toString();
}
}
LeetCode OJ:Add Binary(二进制相加)的更多相关文章
- [LeetCode] 67. Add Binary 二进制数相加
Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...
- leetCode 67.Add Binary (二进制加法) 解题思路和方法
Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...
- [leetcode]67. Add Binary 二进制加法
Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...
- # Leetcode 67:Add Binary(二进制求和)
Leetcode 67:Add Binary(二进制求和) (python.java) Given two binary strings, return their sum (also a binar ...
- LeetCode 67. 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 ...
- [LeetCode] 415. Add Strings 字符串相加
Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...
- [LintCode] Add Binary 二进制数相加
Given two binary strings, return their sum (also a binary string). Have you met this question in a r ...
- 【LEETCODE OJ】Binary Tree Postorder Traversal
Problem Link: http://oj.leetcode.com/problems/binary-tree-postorder-traversal/ The post-order-traver ...
- LeetCode 67 Add Binary(二进制相加)(*)
翻译 给定两个二进制字符串,返回它们的和(也是二进制字符串). 比如, a = "11" b = "1" 返回 "100". 原文 Give ...
随机推荐
- Harbor实现容器镜像仓库的管理和运维
本次分享主要讲述了在开发运维中的管理容器镜像方法.为了便于说明原理,较多地使用Harbor作为例子. 内容主要包括: 开发和生产环境中镜像仓库的权限控制: 镜像远程同步(复制)的原理: 大规模应用镜像 ...
- FORM pdf预览功能函数 SSFCOMP_PDF_PREVIEW
函数模块 SSFCOMP_PDF_PREVIEW Smart Forms: PDF Preview (Test) function ssfcomp_pdf_preview. ...
- shiro的过滤器
shiro的过滤器也是不多的我们可以自定义的方法,它的继承体系如下: 另外UserFilter是继承于AccessControlFilter 1.NameableFilter NameableFilt ...
- [原创] 毕设---在myeclipes中安装Hadoop开发插件
1.安装Hadoop开发插件 hadoop安装包contrib/目录下有个插件hadoop-0.20.2-eclipse-plugin.jar,拷贝到myeclipse根目录下/dropins目录下. ...
- Python3.x:获取登录界面校验码图片
Python3.x:获取登录界面校验码图片 实例代码: # python3 # author lizm # datetime 2018-06-01 18:00:00 # -*- coding: utf ...
- EasyUI:datagrid控件简介
EasyUI:datagrid控件简介 1,水平滚动条属性: //显示滚动条 fitColumns:false //不显示滚动条 fitColumns:true
- 关于Simplicity Studio使用math.h编译出错
原因是未调用C标准库. 解决方法: 1.点项目右键——>properties——>C/C++Build——>Settings——>GNU ARM C Linker——>L ...
- Java JDBC概要总结一(基本操作和SQL注入问题)
JDBC定义: JDBC(Java DataBase Connectivity,java数据库连接)是一种用于执行SQL语句的Java API.JDBC是Java访问数据库的标准规范,可以为不同的关系 ...
- 并发-CopyOnWrite源码分析
CopyOnWrite源码分析 参考: https://blog.csdn.net/linsongbin1/article/details/54581787 http://ifeve.com/java ...
- java中set集合的常用方法
因为Set集合也是继承Collection集合 所以这里就不讲继承Collection集合的方法 都是继承Collection集合的方法 https://www.cnblogs.com/xiaostu ...