408. Add Binary【LintCode java】
Description
Given two binary strings, return their sum (also a binary string).
Example
a = 11
b = 1
Return 100
解题:二进制相加。我的思路是,先转成StringBuilder对象(reverse方法比较好用),因为相加是从最后开始,所以先用reverse方法倒转过来。和上一题类似,用carry变量表示进位,0为不进位,1为需要进一位。最后的结果再倒过来。具体细节标注在代码中,代码如下:
public class Solution {
/**
* @param a: a number
* @param b: a number
* @return: the result
*/
public String addBinary(String a, String b) {
// write your code here
StringBuilder sa = new StringBuilder(a);
StringBuilder sb = new StringBuilder(b);
StringBuilder res = new StringBuilder();
int carry = 0;//表示进位
sa.reverse();
sb.reverse();
int i = 0;
for(i = 0; i < sa.length() && i < sb.length(); i++){
int tpa = sa.charAt(i) - '0';
int tpb = sb.charAt(i) - '0';
if(carry == 0){ // 没有进位
if(tpa == 1 && tpb == 1){
carry = 1;
res.append('0');
}else{
char temp = (char)((int)'0' + (tpa+tpb));
res.append( temp );
}
}else{ //有进位;
if(tpa + tpb == 1){
carry = 1; // 依然有进位
res.append('0');
}else if( tpa + tpb == 2){
carry = 1;
res.append('1');
}else{
// 0 + 0
carry = 0;
res.append('1');
}
}
}
//对剩下的处理
while(i < sa.length()){
//把sa后面的接上去,但是要考虑进位
if(carry == 0){
res.append(sa.substring(i));
break;
}else{
//有进位
if(sa.charAt(i) == '1'){
res.append('0');
}else{
res.append('1');
carry = 0;
}
i++;
}
}
while(i < sb.length()){
//把sa后面的接上去,但是要考虑进位
if(carry == 0){
res.append(sb.substring(i));
break;
}else{
//有进位
if(sb.charAt(i) == '1'){
res.append('0');
}else{
res.append('1');
carry = 0;
}
i++;
}
}
if(carry == 1)
res.append('1');
return res.reverse().toString();
}
}
408. Add Binary【LintCode java】的更多相关文章
- 365. Count 1 in Binary【LintCode java】
Description Count how many 1 in binary representation of a 32-bit integer. Example Given 32, return ...
- 376. Binary Tree Path Sum【LintCode java】
Description Given a binary tree, find all paths that sum of the nodes in the path equals to a given ...
- 375. Clone Binary Tree【LintCode java】
Description For the given binary tree, return a deep copy of it. Example Given a binary tree: 1 / \ ...
- 372. Delete Node in a Linked List【LintCode java】
Description Implement an algorithm to delete a node in the middle of a singly linked list, given onl ...
- 245. Subtree【LintCode java】
Description You have two very large binary trees: T1, with millions of nodes, and T2, with hundreds ...
- 227. Mock Hanoi Tower by Stacks【LintCode java】
Description In the classic problem of Towers of Hanoi, you have 3 towers and N disks of different si ...
- 451. Swap Nodes in Pairs【LintCode java】
Description Given a linked list, swap every two adjacent nodes and return its head. Example Given 1- ...
- 445. Cosine Similarity【LintCode java】
Description Cosine similarity is a measure of similarity between two vectors of an inner product spa ...
- 433. Number of Islands【LintCode java】
Description Given a boolean 2D matrix, 0 is represented as the sea, 1 is represented as the island. ...
随机推荐
- CSS3中为什么要清除浮动以及三种清除浮动(float)的方法
方法一:添加新的元素 .应用 clear:both .clear{ clear:both; height: 0; height: 0; overflow:hidden; } 方法二:父级div定义 o ...
- LWIP network interface 即 LWIP 的 硬件 数据 接口 移植 首先 详解 STM32 以太网数据 到达 的第一站: ETH DMA 中断函数
要 运行 LWIP 不光 要实现 OS 的 一些 接口 ,还要 有 硬件 数据 接口 移植 ,即 网线上 来的 数据 怎么个形式 传递给 LWIP ,去解析 做出相应的 应答 ,2017 ...
- docker 容器不能访问宿主端口原因
因为数据包到了eth0的 上的iptables 表,首先匹配PREROUTING 链,这个拒绝了来自docker0的流量,从而跳到input链,input没有放开服务端口,所以容器访问宿主端口失败;但 ...
- dispatch 之 常见函数小结
你好2019!一起努力呀! 直奔主题 1.dispatch_barrier_async VS dispatch_barrier_sync Barrier blocks only behave spe ...
- SSM(SpringMVC+Spring+Mybatis)框架学习理解
近期做到的项目中,用到的框架是SSM(SpringMVC+Spring+Mybatis).之前比较常见的是SSH.用到了自然得了解各部分的分工 spring mvc 是spring 处理web层请求的 ...
- Vue 源码分析—— 目录结构
一,Vue.js 的源码都是在src 目录下,其目录结构如下. 1.compiler 目录包含Vue.js 所有编译相关的代码.它包括把所有模板解析成ast 语法树, ast 语法树优化等功能. 2. ...
- 利用node中的内置模块fs实现对简单文件的读取 拷贝 创建等功能
1.文件的读取 我们想要根据如下一种目录生成一种json数据 代码如下 //此函苏是对目录进行读取的 //我们想要生成的是一个根据目录所创建的json数据 const fs = require(&qu ...
- 帝国CMS给会员注册加入问答验证
修改文件有e/enews/index.php //注册 elseif($enews=="register") { if($_POST['ask']=='帝国软件') { $user ...
- 嵌入式C语言自我修养 07:地址对齐那些事儿
7.1 属性声明:aligned GNU C 通过 __atttribute__ 来声明 aligned 和 packed 属性,指定一个变量或类型的对齐方式.这两个属性用来告诉编译器:在给变量分配存 ...
- unix文件共享
UNIX系统支持在不同的进程间共享打开文件.内核使用3种数据结构表示打开文件,他们之间的关系决定了在文件共享方面一个进程对另一个进程产生的影响. (1)每个进程在进程表中都有一个记录项,记录项中包含一 ...