leetCode 67.Add Binary (二进制加法) 解题思路和方法
Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100".
思路:二进制加法,比較简单。代码例如以下:
public class Solution {
public String addBinary(String a, String b) {
int len = Math.max(a.length(),b.length());
char[] ch = new char[len + 1];//预防进位
int i = a.length() - 1;
int j = b.length() - 1;
//不考虑进位,所有相加
while(i >= 0 && j >= 0){
ch[len--] = (char) (a.charAt(i--) + b.charAt(j--) - '0');
}
//余下仅有一个还未加完
while(i >= 0){
ch[len--] = a.charAt(i--);
}
while(j >= 0){
ch[len--] = b.charAt(j--);
}
int k = 0;//进位
//处理进位
for(i = ch.length-1; i >=1; i--){
ch[i] = (char) (k + ch[i] - '0');
k = ch[i]/2;
ch[i] = (char) (ch[i]%2 + '0');
}
//推断是否还有进位
ch[0] = (char) (k > 0 ?
k +'0': '0');
//消除开头的0
return new String(ch).replaceAll("^0", "");
}
}
leetCode 67.Add Binary (二进制加法) 解题思路和方法的更多相关文章
- [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 大数加法+字符串处理
题意:两个二进制数相加,大数加法的变形 大数加法流程: 1.倒置两个大数,这一步能使所有大数对齐 2.逐位相加,同时进位 3.倒置两个大数的和作为输出 class Solution { public: ...
- 【LeetCode每天一题】Add Binary(二进制加法)
Given two binary strings, return their sum (also a binary string).The input strings are both non-emp ...
- Java [Leetcode 67]Add Binary
题目描述: Given two binary strings, return their sum (also a binary string). For example,a = "11&qu ...
- (String) leetcode 67. Add Binary
Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...
- [Leetcode] 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). For example,a = "11"b = ...
- 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 ...
随机推荐
- Open Cascade:使用鼠标画线
Open Cascade:使用鼠标画线 在View类文件中创建以下代码: 1.创建鼠标消息: afx_msg void OnLButtonDown(UINT nFlags, CPoint point) ...
- unnamed not found for the web module
intellij idea tomcat 启动报错not found for the web module 使用intellij idea 创建tomcat项目的时候会出现该错误: 启动tomcat的 ...
- java中等待所有线程都执行结束
转自:http://blog.csdn.net/liweisnake/article/details/12966761 今天看到一篇文章,是关于java中如何等待所有线程都执行结束,文章总结得很好,原 ...
- C#中byte类型运算
首先看下面一段代码 byte x = 1; byte y = 2; byte z = x + y; Console.WriteLine(z); 可能很多人会说显示结果是3. 其实,这段代码无法运行,因 ...
- 点击增加删除class
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- luogu P2078 朋友
题目背景 小明在A公司工作,小红在B公司工作. 题目描述 这两个公司的员工有一个特点:一个公司的员工都是同性. A公司有N名员工,其中有P对朋友关系.B公司有M名员工,其中有Q对朋友关系.朋友的朋友一 ...
- Django生成二维码
1. 安装 pip install qrcode 安装Image包 pip install Image 1.1 在代码中使用 import qrcode img = qrcode.make('输入一个 ...
- consul无client模式
1.推consul的镜像到生产应用全部服务器. 每个consul的server模式的容器,都需要单独的物理服务器. 主节点:docker run -d --net=host --name=consul ...
- Linux系统权限
目 录 第1章 权限描述 1 1.1 权限描述 1 1.2 文件权限对应表 1 1.3 三种角色 1 1.4 文件和用户以及组之间的关系 1 第2章 修改权限命令chmo ...
- 【问题探索日志】python 函数优化
# 事情是这样的,我写的一个程序帧率上不去. 然后发现了一个疑似有问题的地方,如下 def around(x,y): around_dict = {(i,j) for i in range(-1,2) ...