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. ...
随机推荐
- DPDK运行出现error while loading shared libraries的解決方法
问题 error: while loading shared libraries: xxx.so.0:cannot open shared object file: No such file or d ...
- C#中如何利用操作符重载和转换操作符 (转载)
操作符重载 有的编程语言允许一个类型定义操作符应该如何操作类型的实例,比如string类型和int类型都重载了(==)和(+)等操作符,当编译器发现两个int类型的实例使用+操作符的时候,编译器会生成 ...
- 用JavaScript中lodash编写双色球
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- MySQL+MyCat分库分表 读写分离配置
一. MySQL+MyCat分库分表 1 MyCat简介 java编写的数据库中间件 Mycat运行环境需要JDK. Mycat是中间件.运行在代码应用和MySQL数据库之间的应用. 前身 : cor ...
- TensorFlow的前世和今生
TensorFlow的前世和今生 TensorFlow是一个开放源码的软件库,用于跨一系列任务的数据流处理编程.TensorFlow是一个符号化的数学应用库,广泛用于机器学习,例如神经网络.在谷歌公司 ...
- js清除浏览器缓存
浏览器缓存 所有的数据都可以存到服务器中,但这样并不高效,当我们访问网页的时候,一会卡顿,二会浪费服务器的存储空间,三会给服务器造成压力 浏览器缓存,可以提高网站性能和浏览器的速度,但对于需要经常更新 ...
- php文件相关操作
//遍历目录及文件 function myBianli($dirname){ //1.打开 $dir = opendir($dirname); //2.读取 while($filename = rea ...
- ls 的详细使用
ls是list的缩写,list--清单 ls 是显示文件 如查看root下的所有信息 如果要以列表的形式显示文件详细信息可使用ls -all或ls -al(包括以“.“开头的隐藏文件) 如果你想查看某 ...
- Linux--ps及top、ls命令
day8 ps系统管理命令 ps是强大的后台进程检测命令 格式:ps [options] [--help] 选项参数: 1.-a :显示所有进程,包括PID等,包括其他用户运行的程序 2.-ef:显 ...
- uni-app 下拉至指定高度固定view
uni.createSelectorQuery().select(‘#salyt’).boundingClientRect(function(rects){ console.log(rects) va ...