【题目】
Given two binary strings, return their sum (also a binary string).
For example,
a ="11"
b ="1"
Return"100"
.
【题意】
给定两个二进制字符串,返回它们的和(也是一个二进制字符串)。
【分析】
类似大数加法
【代码】
/*********************************
* 日期:2014-02-05
* 作者:SJF0115
* 题号: 67.Add Binary
* 网址:http://oj.leetcode.com/problems/add-binary/
* 结果:AC
* 来源:LeetCode
* 总结:
**********************************/
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
class Solution {
public:
string addBinary(string a, string b) {
int lenA = a.length();
int lenB = b.length();
if(lenA == 0){
return b;
}
else if(lenB == 0){
return a;
}
int max = lenA > lenB ? (lenA + 2) : (lenB + 2);
char* result = new char[max];
int index = 0;
int i = lenA-1,j = lenB-1;
int numA,numB,c = 0,sum = 0;
//加法
while(i >= 0 || j >= 0){
numA = i >= 0 ? (a[i] - '0') : 0;
numB = j >= 0 ? (b[j] - '0') : 0;
sum = numA + numB + c;
c = sum / 2;
result[index++] = sum % 2 + '0';
i--;
j--;
}
//最后一个进位
if(c > 0){
result[index++] = c + '0';
}
result[index] = '\0';
//反转
for(i = 0,j = index - 1;i < j;i++,j--){
char temp = result[i];
result[i] = result[j];
result[j] = temp;
}
return string(result);
}
};
int main() {
Solution solution;
string a = "1010";
string b = "1011";
string str = solution.addBinary(a,b);
cout<<str<<endl;
return 0;
}
【代码2】
class Solution {
public:
string addBinary(string a, string b) {
int lenA = a.length();
int lenB = b.length();
if(lenA == 0){
return b;
}
else if(lenB == 0){
return a;
}
string result;
int index = 0;
int i = lenA-1,j = lenB-1;
int numA,numB,c = 0,sum = 0;
//加法
while(i >= 0 || j >= 0 || c > 0){
numA = i >= 0 ? (a[i] - '0') : 0;
numB = j >= 0 ? (b[j] - '0') : 0;
sum = numA + numB + c;
c = sum / 2;
result.insert(result.begin(),sum % 2 + '0');
i--;
j--;
}
return result;
}
};
【代码3】
class Solution {
public:
string addBinary(string a, string b) {
int lenA = a.length();
int lenB = b.length();
string result;
int index = 0;
int i = lenA-1,j = lenB-1;
int sum = 0;
//加法
while(i >= 0 || j >= 0){
sum += i >= 0 ? (a[i] - '0') : 0;
sum += j >= 0 ? (b[j] - '0') : 0;
result = ((sum & 1) ? "1" : "0") + result;
sum >>= 1;
i--;
j--;
}
return sum ? "1" + result : result;
}
};
分享到:
相关推荐
leetCode 第67题。Swift解
Add Two Numbers], Linked list 2017.06.13 打卡[LeetCode 200. Number of Islands], BFS 2017.06.14 打卡[LeetCode 3. Longest Substring Without Repeating Characters], N/A 2017.06.15 打卡[LeetCode 407. ...
401 | [Binary Watch](https://leetcode.com/problems/binary-watch/) | [C++](./C++/binary-watch.cpp) [Python](./Python/binary-watch.py) | _O(1)_ | _O(1)_ | Easy | | 411 | [Minimum Unique Word ...
LeetCode去除数组重复元素 Arithmetic-Swift 一些算法的swift实现 桶排序 冒泡排序 快速排序 ##正好看见LeetCode可以刷Swift的题目 开始慢慢刷 swift有playground 做起来还是相当方便的 已完成题目 ----2016.9.30 两...
LeetCode笔记本docsifyjsLeetCode算法Java c / c ++ javascript的基本知识简单的1. Two Sum 704. Classical Binary Search2. 3 Sum206. Reverse Linked List中等的33. Search in Rotated Sorted Array74. Search a ...
两数相加(Add Two Numbers) 2018.9.25 翻转二叉树(Invert Binary Tree) 2018.9.25 环形链表(Linked List Cycle) 2018.9.25 环形链表 II(Linked List Cycle II) 2018.9.26 删除排序链表中的重复元素 II...
Algorithm templates and LeetCode SolutionsTBD Add multi binary search algorithm for leetcode0034 Add Segment Tree for data structure Add union-find algorithm(Disjoint Set Union) for data structure Add...
67.二进制求和 (Add Binary) 69.x 的平方根 (Sqrt(x)) 70.爬楼梯 (Climbing Stairs) 83.删除排序链表中的重复元素 (Remove Duplicates from Sorted List) 88.合并两个有序数组 (Merge Sorted Array) 100.相同的树 ...
2.Add Two Numbers 3.Longest Substring Without Repeating Characters 4.Median of Two Sorted Arrays 5.Longest Palindromic Substring 6.ZigZag Conversion 7.Reverse Integer 8.String To Integer 9.Palindrome ...
BinaryAdd.java,给定两个二进制字符串,返回他们的和,leetcode 47. BinaryInsertSort.java,二分插入排序。 BinaryTree.java,二叉树相关,包括遍历删除等方法。 ChessBoard.java,棋盘覆盖算法。 Dijkstra.java,...
leetcode双人赛LeetCode ...AddBinary :简单的问题。 AddTwoNumbers :简单的问题。 Anagrams :简单的#hashtable问题。 BalancedBinaryTree :简单的#balance #tree问题。 BestTimetoBuyandSellStock :简单的问题。 ...
add-binary │ └── Solution.665166.java ├── add-two-numbers │ └── Solution.666385.java ├── balanced-binary-tree │ └── Solution.660938.java ├── best-time-to-buy-and-sell-stock
leetcode中文版车鸟 一组用python编写的算法。 种类 冒泡排序 插入排序 归并排序 桶排序 计数排序 基数排序 选择排序 快速排序 堆排序 搜索 二分查找 深度优先搜索 广度优先搜索 递归 json 漂亮的打印 过滤中文文件 ...
add-two-numbers find-peak-element longest-common-subsequence longest-consecutive-sequence max-area-of-island next-greater-element-ii serialize-and-deserialize-binary-tree subarray-sum-equals-k binary-...
Programming),或是二分查找(Binary Search),或是回溯(Back tracing),或是分治法(Divide and Conquer),还有大量的对 树,数组、链表、字符串 和 hash 的操作。通过做这些题能让你对这些最基础的算法的思路...
leetcode ...Binary Linked-list 002 Add Two Numbers Stack 020 Valid Parenthesis Hash Table 001 TwoSum Reference 完整的学习流程 How to be a softwair engineer: 其他人详解 Python的各式演算法
leetcode 答案 LeetCode-Trip LeetCode刷题代码,大佬勿入。 为一年后的研究生找工作...Binary Tree] [121. Best Time to Buy and Sell Stock] [167. Two Sum II - Input array is sorted] Medium [2. Add Two Numbers]
addBinary longestPalindrome maximal rectangle :dp问题,较难 largestRectangleArea 求直方图的最大面积,左右两次扫面+剪枝优化 Valid Parentheses 用栈判断括号匹配 Regular Expression Matching 递归匹配 ...
Leetcode的ac是什么意思 LeetCodeInJava List #98 Validate Binary Search Tree #100 Same Tree #104 Maximum Depth of Binary Tree #122 Best Time to Buy and Sell Stock II #136 Single Number #150 Evaluate ...
王一飞的LeetCode题解 算法: | 问题 | 解决方案 | -----|---------------- | --------------- | 第344话| [ReverseString.cc] (./solutions/ReverseString.cc) | 第292话| [NimGame.cc] (./solutions/NimGame.cc) |...