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 ...
随机推荐
- ContentProvider 增删改查通讯录
一.通讯录应用介绍 通讯录应用是Android自带的应用程序,我们看到此应用的时候,可能只认为这是一个应用,用数据库存储数据,但是实际上不是这样的. 通讯录是ContentProvider的应用,通讯 ...
- WPF中获得控件相对于控件的相对位置
GeneralTransform generalTransform = lstitem.TransformToAncestor(this.BackStack); Point point = gener ...
- 2015/9/9 js继续学习
var book={ //对象是由花括号括起来的 topic:“JavaScript”://属性“topic”的值是“JavaScript” fat:true: ...
- ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) 背景: mys ...
- 字符串复制strncpy
#include "stdafx.h" #include "iostream" #include "assert.h" using name ...
- Unity多线程(Thread)和主线程(MainThread)交互使用类——Loom工具分享
Unity多线程(Thread)和主线程(MainThread)交互使用类——Loom工具分享 By D.S.Qiu 尊重他人的劳动,支持原创,转载请注明出处:http.dsqiu.iteye.com ...
- Js之Dom学习-三种获取页面元素的方式、事件、innerText和innerHTML的异同
一.三种获取页面元素的方式: getElementById:通过id来获取 <body> <input type="text" value="请输入一个 ...
- JS初级-作用域
作用域:域:空间.范围.区域--作用:读.写 script 全局变量.全局函数 自上而下 函数 由里到外 {} 浏览器 ...
- 一个创建Coco2d-x项目的脚本
1.使用环境 我测试的环境是Mac OS 10.10 +Coco2d-x 3.2,是使用shell写的脚本,应该linux/unix都应该 可以使用. 2.使用可能出现的问题 使用中可能会爆权限不足的 ...
- HTTPD服务 openssl的https服务机制
环境: 环境: httpd服务器:10.140.165.169 CA服务器:10.140.165.93 CA服务器配置: 1.安装openssl [root@cnhzdhcp16593 ~]# yum ...