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 ...
随机推荐
- 购物单问题—WPS使用excel
**** 180.90 88折 **** 10.25 65折 **** 56.14 9折 **** 104.65 ...
- CKEditor的下载、配置与使用
CKEditor简介: CKEditor 是一款功能强大的开源在线文本编辑器.它所见即所得的特点,使你在编辑时所看到的内容和格式,能够与发布后看到的效果完全一致.CKEditor 完全是基于 Java ...
- c# 图片加密解密的实例代码
c# 图片加密解密的实例代码. 代码: using System; using System.Collections.Generic; using System.Text; using System. ...
- Oracle_trunc截取函数
转:http://blog.sina.com.cn/s/blog_6b58d2fa0100r6ub.html TRUNC函数用于对值进行截断. 用法有两种:TRUNC(NUMBER)表示截断数字,TR ...
- LeetCode:路径总和II【113】
LeetCode:路径总和II[113] 题目描述 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例:给定如下二叉树, ...
- PAT 天梯赛 L1-022. 奇偶分家 【水】
题目链接 https://www.patest.cn/contests/gplt/L1-022 AC代码 #include <iostream> #include <cstdio&g ...
- LeetCode: Max Consecutive Ones
这题最关键的是处理最开始连续1和最后连续1的方式,想到list一般在最前面加个node的处理方式,在最前面和最后面加0即可以很好地处理了 public class Solution { public ...
- Linux系统下wget命令的使用教程
一.Linux wget简介 wget是linux上的命令行的下载工具.这是一个GPL许可证下的自由软件.Linux wget支持HTTP和FTP协议,支持代理服务器和断点续传功能,能够自动递归远程主 ...
- timer使用方法
, HEART_EXPIRE}; , }; /* 第一次调用要多长时间 */ struct itimerval it; it.it_interval = tv_interval; it.it_valu ...
- 2017版:KVM 性能优化之内存优化
我们说完CPU方面的优化,接着我们继续第二块内容,也就是内存方面的优化.内存方面有以下四个方向去着手: EPT 技术 大页和透明大页 KSM 技术 内存限制 1. EPT技术 EPT也就是扩展页表,这 ...