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 ...
随机推荐
- Sql server用QQ邮箱发送邮件
一.配置数据库邮件 https://jingyan.baidu.com/article/3ea51489a135f752e71bba5b.html
- PAT 1119 Pre- and Post-order Traversals [二叉树遍历][难]
1119 Pre- and Post-order Traversals (30 分) Suppose that all the keys in a binary tree are distinct p ...
- Spark生态系统剖析--王家林老师
- table实现 js数据访问 传递json数据用render_to_response
$(document).ready(function(){ $.ajax({ url:'/query/', dataType:'json', type:'GET', success:function( ...
- 系统间接口联调总是报500 for URL 和 乱码
两个系统做数据传输时,懒省事,直接访问 action 方式.结果总是报500,或者fileNotFount. 究其原因是因为两边的数据格式没对应上.post请求返回的格式是String,数据提供方返回 ...
- GIT学习笔记(4):远程分支
GIT学习笔记(4):远程分支 远程分支 远程分支是什么 远程分支是对远程仓库中的分支的索引.它们是一些无法移动的本地分支:只有在GIT进行网络交互时才会更新.远程分支就是书签,提醒着你上次连接远程仓 ...
- alias指令别名和 sshpass命令简化ssh登陆
在之前的一篇博文中 ubuntu下关于profile和bashrc中环境变量的理解 提到过可以编辑bashrc文件,vim ~/.bashrc,来编写自己的小指令,就是给长指令取个简单的别名.比如b ...
- Kotlin学习记录1
参考我的博客:http://www.isedwardtang.com/2017/09/02/kotlin-primer-1/
- shell文件安全与权限 笔记
主要学习: 文件盒目录的权限 Setuid Chown和chgrp Umask 连接符号 一个文件已经创建,就具有三种访问方式 读,可以显示该文件的内容 写,可以编辑或删除它 执行,如果该文件时一个s ...
- zabbix监控nginx的性能
1.nginx配置 需要使用zabbix监控nginx,首先nginx需要配置ngx_status,在nginx的配置文件中加入红框中的配置,然后重启nginx如下图所示: location /ngx ...