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 ...
随机推荐
- Nginx+Springboot+Vue 前后端分离 解决跨域问题
1:前端vue 写完 打包 npm run build prod 2: 后端api 写完打包 springboot mvn package -Dmaven.test.skip=true 3: ngin ...
- win7开启特定端口
win7开启特定端口 在xp系统的时代,修改防火墙很方便,很简单.windows7或许是做得过于复杂了.当然所谓安全性也是相当于其他之前版本的系统更高了.为什么要打开端口,肯定是在win ...
- Hbase 学习笔记1----shell
Hbase 是一个分布式的.面向列的开源数据库,其实现是建立在google 的bigTable 理论之上,并基于hadoop HDFS文件系统. Hbase不同于一般的关系型数据库(RDBMS ...
- mysql双向主从同步
双向主从同步 双方互相主从同步配置 然后再my.cnf中加上如下配置 [mysqld]master1:auto_increment_increment = 2 //自增ID的间隔,如1 3 5间隔为2 ...
- 一行代码实现笔记本跳过微信认证连接WIFI
一行代码实现笔记本跳过微信认证连接WIFI 本文作者原创,没有参考其他文章,方法很简单但是很实用,转载请注明出处,谢谢! 问题 有一些WIFI需要通过微信认证才能连接,手机当然是可以的,但是我们手头的 ...
- HIVE大数据出现倾斜怎么办
hive在跑数据时经常会出现数据倾斜的情况,使的作业经常reduce完成在99%后一直卡住,最后的1%花了几个小时都没跑完,通过YARN的管理界面配合日志,可以清楚其中的具体原因,这种情况就很可能 ...
- leetcode每日一题——两数之和
题目: 两数之和 难度: 简单 描述: 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 解法: class Solutio ...
- ado.net(增删改)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- Java 基础总结(二)
本文参见:http://www.cnblogs.com/dolphin0520/category/361055.html 1. 字节流与和字符流 1). 字符流操作时使用了缓冲区,而在关闭字符流时会强 ...
- Sybase:delete与truncate、drop区别
Sybase:delete与truncate.drop区别 区别: TRUNCATE TABLE TABLENAME:删除内容.释放空间但不删除定义. DELETE FROM TABLENAME:删除 ...