Leetcode 67 Add Binary 大数加法+字符串处理
题意:两个二进制数相加,大数加法的变形
大数加法流程:
1.倒置两个大数,这一步能使所有大数对齐
2.逐位相加,同时进位
3.倒置两个大数的和作为输出
class Solution {
public:
string addBinary(string a, string b) {
if(a.size() < b.size()){
string t = a;
a = b;
b = t;
} //该步使的能大数加小数,使其能加
reverse(a.begin(),a.end());
reverse(b.begin(),b.end());//倒置两数
for(string::size_type i = ; i < a.size(); ++i){
a[i] += ((i < b.size())? b[i] : '') - '';//逐位相加
}
for(string::size_type i = ; i < a.size() - ; ++i){
if(a[i] >='' ) {
a[i] -= ;
a[i+] ++;
}
}
if(a[a.size()-] >='' ) {
a[a.size()-] -= ;
a += "";
} //进位
reverse(a.begin(),a.end()); //倒置输出
return a;
}
};
Leetcode 67 Add Binary 大数加法+字符串处理的更多相关文章
- 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 (高精度加法)
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...
- (String) 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(二进制相加)(*)
翻译 给定两个二进制字符串,返回它们的和(也是二进制字符串). 比如, a = "11" b = "1" 返回 "100". 原文 Give ...
- 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 ...
- Java [Leetcode 67]Add Binary
题目描述: Given two binary strings, return their sum (also a binary string). For example,a = "11&qu ...
随机推荐
- android笔记:ViewPager实现界面的滑动
最近在学习ViewPager实现界面的滑动,拜读了郭神的博客文章,并抽取归纳了自己对ViewPager的理解. ViewPager实现界面滑动的步骤如下: 1.在xml布局内加入控件android.s ...
- ZT 第一范式,第二范式,第三范式
第一范式,第二范式,第三范式 Posted on 2012-05-09 16:30 GISerYang 阅读(6472) 评论(0) 编辑 收藏 第一范式 存在非主属性对码的部分依赖关系 R(A,B, ...
- Oracle题目
1. 创建一个函数fun_sal,该函数根据部门号获得该部门下所有员工的平均工资Create or replace function fun_sal(deptnos number)return var ...
- ADB理解
在做手机测试时候,经常用到的命令就是adb.如adb shell,adb devices,adb logcat等等 那么什么是adb,怎么用呢? 一.adb adb的全称为Android Debug ...
- Jmeter应用初步介绍
一.工具介绍 Apache JMeter是Apache组织开发的基于Java的压力测试工具.用于对软件做压力测试,它最初被设计用于Web应用测试,但后来扩展到其他测试领域. 它可以用于测试静态和动态资 ...
- Schema约束
Schema约束(*xml中如何引入schema约束)(看懂Schema:能根据Schema写出XML文档来:)1.Schema约束文档本身就是一个XML文档.2.Schema对名称空间支持很好3.S ...
- MapReduce: 一种简化的大规模集群数据处理法
(只有文字没有图,图请参考http://research.google.com/archive/mapreduce.html) MapReduce: 一种简化的大规模集群数据处理法 翻译:风里来雨里去 ...
- netbeans中实体类代码的bug
用了netbeans中实体类代码后,忽然报错: com.sun.tools.javac.code.Symbol$CompletionFailure: 找不到sun.util.logging.Platf ...
- 【树上莫队】【带修莫队】【权值分块】bzoj4129 Haruna’s Breakfast
#include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using ...
- JAVA_BaseDAO数据处理类
package dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStat ...