leetCode->Add Binary
来源:互联网 发布:知乎 被离职 编辑:程序博客网 时间:2024/06/05 15:13
<span style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;">每天一刷,强迫学习</span>
Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100"
.
这道题乍看很简单,将输入的二进制字符串转换为int(利用parseInt(String s, int radix)
),计算结果后结果用Integer.toBinaryString(int)转换为二进制字符串即可。提交时测试用例不通过,原因是Integer只能表示32位的数字,此时查找Java有BigInteger类。
总结:
1、注意输入输出边界;
2、java中BigInteger类可以表示大整数。其中构造函数支持多种进制的字符串。
import java.math.BigInteger;public class Solution {public String addBinary(String a, String b) {BigInteger aValue = new BigInteger(a, 2);BigInteger bValue = new BigInteger(b, 2);BigInteger sum = new BigInteger("0", 2);sum = aValue.add(bValue);return sum.toString(2);}}
0 0
- LeetCode: Add Binary
- [Leetcode] Add Binary
- LeetCode Add Binary
- Leetcode: Add Binary
- [Leetcode] Add Binary
- LeetCode : Add Binary
- LeetCode Add Binary
- [LeetCode] Add Binary
- leetcode 58: Add Binary
- leetcode Add Binary
- [LeetCode] Add Binary
- 【leetcode】Add Binary
- [LeetCode]Add Binary
- [leetcode]Add Binary
- LeetCode-Add Binary
- [leetcode] Add Binary
- LeetCode - Add Binary
- leetcode:Add Binary
- MFC之静态调用DLL实现编辑ini配置文件
- A - The Dole Queue
- 封装重复工作——判断文本框是否为空
- poj 2115 C Looooops
- Django配置Mysql数据库详解
- leetCode->Add Binary
- set容器总结
- 计算机面试书籍与求职网站推荐
- JX8NET小游戏网公司面试一整套的流程
- ASP.NET MVC的流程讲解
- 批量删除其他用户
- IOS 时间格式 时间转换 大总结
- arcgis for android 平台搭建
- ReportStudio入门教程(六十六) - 文本源变量(动态修改列标题)