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. ...
随机推荐
- Eclipse常用快捷键(持续更新)
编辑相关快捷键 Eclipse的编辑功能非常强大,掌握了Eclipse快捷键功能,能够大大提高开发效率.Eclipse中有如下一些和编辑相关的快捷键. 1.[Ctrl+O] 显示类中方法和属性的大纲, ...
- Boost asio基本概念
asio库基于操作系统提供的异步机制,采用前摄器模式(Proactor)实现可移植的异步(或同步)IO操作,不需要使用多线程和锁,有效避免多线程编程带来的诸多有害副作用(如竞争,死锁). asio封装 ...
- 无法加载文件或程序集“Newtonsoft.Json”或它的某一个依赖项
未能加载文件或程序集“Newtonsoft.Json”或它的某一个依赖项.找到的程序集清单定义与程序集引用不匹配. (异常来自 HRESULT:0x80131040). 有时候我们创建了一个类库,我们 ...
- CSS 学习路线(二)选择器
选择器 规则结构: 分两个基本部分 选择器(selector)和声明块(declaration block) 组成 声明块:由一个或多个声明组成,每一个声明都是属性-值对 选择器分为:元素选择器,类选 ...
- BZOJ 5248: [2018多省省队联测]一双木棋(对抗搜索)
Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 439 Solved: 379[Submit][Status][Discuss] Descriptio ...
- MySql 5.7.21免安装版本win10下的配置
1.解压到想要安装的位置,创建my.ini文件 my.ini的内容如下 [mysql] # 设置mysql客户端默认字符集 default-character-set=utf8 [mysqld] #设 ...
- shell习题第4题:监控ip地址存活
[题目要求] 设计一个脚本,监控远程的一台机器(ip为192.168.1.100)的存活状态,当发现宕机的时候发一份邮件给自己 [核心要点] ping -c10 192.168.1.100通过 pin ...
- 【原创】CRM 2015/2016,SSRS 生成PDF文件,幷以附件的形式发送邮件
主要步骤如下: 生成一条邮件记录 生成一条ActivityParty记录 生成PDF文件,并以Base64添加到ActivityMimeAttachment 中去 打开发送邮件窗口,以便编辑及发送邮件 ...
- centos7 关闭防火墙
centos7 关闭防火墙 1.firewall相关的操作 查看防火墙状态 firewall-cmd --state 关闭防火墙 systemctl stop firewalld.s ...
- 从零开始一个http服务器(五)-模拟cgi
从零开始一个http服务器-模拟cgi(五) 代码地址 : https://github.com/flamedancer/cserver git checkout step5 运行: make cle ...