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 ...
随机推荐
- html css一些记录
1.忽略将页面中的数字识别为电话号码 <meta content="telephone=no" name="format-detection" /> ...
- Cannot fetch index base URL https://pypi.python.org/simple/
这个就是相源的问题,正常安装你的根目录下会有这个pip.log文件,如下 root@liu:~# ll .pip/ total 16 drwxr-xr-x 2 root root 4096 Sep 1 ...
- cookie 的“Value”=“xxxxx,xxxxx”部分无效
cookie 的“Value”=“xxxxx,xxxxx”部分无效 在一些网站中有时候会遇到Cookie的值为逗号 但是在.Net中Cookie的值是不能直接使用逗号的 如果使用形如 C#代码 1.C ...
- centos6搭建VPN
1,检查是否开启PPP #cat /dev/ppp cat: /dev/ppp: No such device or address //表示已经开启 2,安装ppp和iptables #yum in ...
- vnc 登录后只有终端 没有桌面 黑屏
1, start vnc server: vncserver :1 issue: connect it with pc and only display one terminal. 2, stop v ...
- C# 基础(2)
打开一个解决方案,以.sin后缀名,.csproj是项目文件的后缀名. Console.WriteLine("这是我的第二个项目!");你想显示的内容 Console.ReadKe ...
- Java类路径
Java 类路径告诉 java 解释器和 javac 编译器去哪里找它们要执行或导入的类. 类(您可能注意到的那些 *.class 文件)可以存储在目录或 jar 文件中,或者存储在两者的组合中, 但 ...
- maven错误:Project configuration is not up-to-date with pom.xml
原因: 1.导入maven工程后,出现如下错误: Description Resource Path Location TypeProject configuration is ...
- 《CSS3专业网页开发指南》笔记
书本:<CSS3专业网页开发指南>(the book of css3) Peter Gasston 著 李景媛 吴晓嘉 译 第1章: 1.box-sizing : IE8及以上版本 ...
- cmd
ExecuteNonQuery 返回影响的行数 ExecuteScalar 返回第一行第一列