【leetcode刷题笔记】Add Binary
Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100"
.
题解:简单的二进制加法模拟。a,b的最后以为对齐开始进行加法,用carries保存进位,如果加完后最高位还有进位,那么要在结果的最前面加一个1。
代码如下:
public class Solution {
public String addBinary(String a, String b) {
if(a == null || a.length() == 0)
return b;
if(b == null || b.length() == 0)
return a; String s = new String();
int carries = 0;
int a_kepeler = a.length()-1;
int b_kepeler = b.length()-1; while(a_kepeler >= 0 && b_kepeler >= 0){
int sum = carries + a.charAt(a_kepeler) - '0' + b.charAt(b_kepeler) - '0';
carries = sum / 2;
sum = sum % 2;
s = String.valueOf(sum) + s;
a_kepeler--;
b_kepeler--;
} while(a_kepeler >= 0){
int sum = carries + a.charAt(a_kepeler) - '0';
carries = sum / 2;
sum = sum % 2;
s = String.valueOf(sum) + s;
a_kepeler--;
} while(b_kepeler >= 0){
int sum = carries + b.charAt(b_kepeler) - '0';
carries = sum / 2;
sum = sum % 2;
s = String.valueOf(sum) + s;
b_kepeler--;
} if(carries > 0)
s = "1" + s; return s;
}
}
上述代码还可以优化,就是如果a的长度小于b,就把a,b交换,使得a总是较长的那个,那么就可以省略第30~36行的while循环了。
【leetcode刷题笔记】Add Binary的更多相关文章
- 【leetcode刷题笔记】Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- LeetCode刷题笔录Add Binary
Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...
- 【leetcode刷题笔记】Binary Tree Level Order Traversal(JAVA)
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- 【leetcode刷题笔记】Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- 【leetcode刷题笔记】Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- LeetCode刷题笔记和想法(C++)
主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...
- 18.9.10 LeetCode刷题笔记
本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...
- LeetCode刷题笔记 - 12. 整数转罗马数字
学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...
- LeetCode练题——67. Add Binary
1.题目 67. Add Binary——easy Given two binary strings, return their sum (also a binary string). The inp ...
- Leetcode刷题笔记(双指针)
1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...
随机推荐
- WebLogicSSL解决苹果IOS itms下载问题
前提: 安装好openssl工具. 通过windows cmd 命令进入 openssl工作文件夹 如解压文件夹为:E:\ openssl-1.0.2-beta1 则工作文件夹为E:\openssl- ...
- leetcode笔记:Add Binary
一.题目描写叙述 Given two binary strings, return their sum (also a binary string). For example, a = "1 ...
- Android环境变量的设置(详细图解版)
分类: Android初学学习笔记2011-07-10 09:47 99479人阅读 评论(0) 收藏 举报 androidtoolspathcmd 查阅了网上很多的资料但是对于环境变量设置介绍的不够 ...
- jfreechart折线图 demo
public class ChartUtil { public static ChartUtil chartUtil; private RoomViewsDataService roomViewsDa ...
- 《Lucene in Action》(第二版) 第一章节的学习总结 ---- 用最少的代码创建索引和搜索
第一章节是介绍性质,但是通过这一章节的学习,我理解到如下概念: 1.Lucene由两部分组成:索引和搜索.索引是通过对原始数据的解析,形成索引的过程:而搜索则是针对用户输入的查找要求,从索引中找到匹配 ...
- Java集合系列之TreeMap源代码分析
一.概述 TreeMap是基于红黑树实现的.因为TreeMap实现了java.util.sortMap接口,集合中的映射关系是具有一定顺序的,该映射依据其键的自然顺序进行排序或者依据创建映射时提供的C ...
- python 求下个月的最后一天
[1]根据当前月求上个月.下个月的最后一天 (1)求当前月最后一天 (2)求前一个月的最后一天 (3)求下一个月的最后一天 学习示例与应用实例,代码如下: #!/usr/bin/python3 #-* ...
- unity 常用的几种相机跟随
固定相机跟随 这种相机有一个参考对象,它会保持与该参考对象固定的位置,跟随改参考对象发生移动 using UnityEngine; using System.Collections; public c ...
- iOS 动画基础总结篇
iOS 动画基础总结篇 动画的大体分类(个人总结可能有误) 分类.png UIView 动画 属性动画 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1 ...
- Miller-Rabin大素数测试模板
根据费马小定理: 对于素数n,a(0<a<n),a^(n-1)=1(mod n) 如果对于一个<n的正整数a,a^(n-1)!=1(mod n),则n必不是素数. 然后就可以随机生成 ...