AddBinary
二进制加法
输入2个字符串,字符串内由0和1组成;计算二者之和,返回字符串
Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100".
package com.rust.TestString;
public class AddBinary {
public static String addBinary(String a, String b) {
int alen = a.length() - 1;
int blen = b.length() - 1;
int carry = 0;
String res = "";
while (alen >=0 || blen >= 0 || carry == 1){
int delta = (alen < 0)? 0 : a.charAt(alen--) - '0';/* 得到int */
int beta = (blen < 0)? 0 : b.charAt(blen--) - '0';
res = (char)('0' + delta ^ beta ^ carry) + res;/* 异或处理得到当前位 */
carry = (delta + beta + carry) >> 1;/* 移位处理得到进位 */
}
return res;
}
public static void main(String args[]){
String text = "0101010";
String atext = "111";
String btext = "010";
System.out.println(addBinary(atext, text));
System.out.println(addBinary(atext, btext));
System.out.println(addBinary(btext, text));
}
}
输出:
0110001
1001
0101100
AddBinary的更多相关文章
- leetcode — add-binary
/** * Source : https://oj.leetcode.com/problems/add-binary/ * * * Given two binary strings, return t ...
- add-binary 字符串操作,二进制字符串相加
Given two binary strings, return their sum (also a binary string). For example,a ="11"b =& ...
- [LeetCode] Add Binary 二进制数相加
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...
- Leetcode分类刷题答案&心得
Array 448.找出数组中所有消失的数 要求:整型数组取值为 1 ≤ a[i] ≤ n,n是数组大小,一些元素重复出现,找出[1,n]中没出现的数,实现时时间复杂度为O(n),并不占额外空间 思路 ...
- leetcode算法分类
利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...
- 全部leetcode题目解答(不含带锁)
(记忆线:当时一刷完是1-205. 二刷88道.下次更新记得标记不能bug-free的原因.) 88-------------Perfect Squares(完美平方数.给一个整数,求出用平方数来 ...
- 【leetcode】Add Binary
题目简述: Given two binary strings, return their sum (also a binary string). For example, a = "11&q ...
- leetcode刷题全纪录(持续更新)
2.Add Two Numbers 原题链接https://leetcode.com/problems/add-two-numbers/ AC解: public ListNode addTwoNumb ...
- LeetCode---String
Count and Say 思路:递归求出n - 1时的字符串,然后双指针算出每个字符的次数,拼接在结果后面 public String countAndSay(int n) { if(n == 1) ...
随机推荐
- js 玩一玩
闲着没事学了学js,做了一个下页面玩玩. 下面是html代码: <!DOCTYPE html><html> <head> <meta charset=&quo ...
- Nodejs-express 4.0框架 简单介绍
http://www.expressjs.com.cn/4x/api.html 这个是express API的中文手册 但是只是翻译了一点 英语比较差比较难受. 1. 关于安装 现在网上查的时候有一 ...
- Xamarin控件使用之GridView
[Activity(Label = "MainGridViewActivity", LaunchMode = LaunchMode.SingleTop)]//设置Activity启 ...
- 纯手工打造简单分布式爬虫(Python)
前言 这次分享的文章是我<Python爬虫开发与项目实战>基础篇 第七章的内容,关于如何手工打造简单分布式爬虫 (如果大家对这本书感兴趣的话,可以看一下 试读样章),下面是文章的具体内容. ...
- php 面向对象的三大特性
<?phpheader("Content-type:text/html;charset=utf-8");/*封装目的:为了使类更加安全做法:1.将成员变量变成私有2.做一个成 ...
- 备份Rhythmbox播放器的曲目和播放列表信息
Rhythmbox音乐播放器只能保存单个播放列表,如果在rhythmbox下建了很多播放列表(比如按歌手名分类),每个播放列表下包含一些歌曲,为了避免重装系统后重新建这些播放列表,可以备份下面的文件. ...
- Java IO学习笔记八
BufferedReader和BufferedWriter 这两个类是高效率的提高文件的读取速度,它们为字符输入和输出提供了一个缓冲区,可以显著的调高写入和读取的速度,特别针对大量的磁盘文件读取的时候 ...
- Scrapyd部署爬虫
Scrapyd部署爬虫 准备工作 安装scrapyd: pip install scrapyd 安装scrapyd-client : pip install scrapyd-client 安装curl ...
- 在PHP中连接数据库的八大步骤
连接数据库的八大步骤 Step1:链接数 ...
- 设置Ubuntu下adb 及 fastboot权限
以普通用户登录linux,然后运行adb devices会提示权限不够: List of devices attached ???????????? no permissions 这是因为 ...