2017.10.23 LeetCode
来源:互联网 发布:安卓翻墙软件fqrouter2 编辑:程序博客网 时间:2024/06/03 04:20
67. Add Binary
Description
Given two binary strings, return their sum (also a binary string).
For example,
a = “11”
b = “1”
Return “100”.
题意: 给你两个二进制的字符串,让你返回二进制的和
分析 : 也可以暴力的模拟枚举各种情况,但是太复杂了,还得考虑进位的情况,一开始用这种思路太麻烦了,就放弃了,后来看了别人的思路发现,很巧妙,学习下,还是得设置一个进位,我们利用这个进位来进行判断,首先从低位开始,将两个数都和进位相加即可,详细代码
参考函数
class Solution {public: string addBinary(string a, string b) { int lenA = a.size() - 1; int lenB = b.size() - 1; int s = 0; string ans; while (lenA >= 0 || lenB >= 0 || s) { s += lenA >= 0?a[lenA--]-'0':0; s += lenB >= 0?b[lenB--]-'0':0; ans = (char)(s%2 + '0') + ans; s /= 2; } return ans; }};
阅读全文
0 0
- 2017.10.23 LeetCode
- LeetCode | # 23
- Leetcode#23||
- Leetcode 23
- LeetCode #23
- 2017.10.21 LeetCode
- 2017.10.22 LeetCode
- 2017.10.25 LeetCode
- 2017.10.27 LeetCode
- 2017.10.28 LeetCode
- 2017.10.29 LeetCode
- 2017.10.30 LeetCode
- 2017.10.31 LeetCode
- [LeetCode] Sort Colors [23]
- [leetcode 23] Generate Parentheses
- [leetcode]23 Path Sum
- leetcode #23 in cpp
- leetCode练习(23)
- fragment中调用getActivity为null的几种情况
- 无侧拉
- 51nod 1122 机器人走方格 V4[矩阵快速幂]
- 二、MySQL的操作
- java.io.IOException: Could not locate executable null\bin\winutils.exe in the Hadoop binaries.
- 2017.10.23 LeetCode
- js翻页效果实现
- 欢迎使用CSDN-markdown编辑器
- Banner
- 指纹识别
- 学习使用NavigationView,实现QQ侧滑菜单
- 如何自制思维导图
- 配置C3P0连接池,项目记录
- ringbuffer