- 浏览: 174429 次
- 性别:
- 来自: 济南
文章分类
最新评论
Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100".
这道题目与Add Two Numbers 这道题目类似。不同的是这道题符合我们从低位到高位的进位方式,也就是从右往左,add two numbers那道题目是从左往右,一个是十进制,一个是二进制。思路是一样的,维护一个进位carry,对于这道题目,如果当前的值大于1的时候就需要进位,此时我们就把carry设置为1。在我们扫描完字符串之后,不要忘记检测carry的值,如果为1我们就要把它加入新的字符串中。代码如下:
For example,
a = "11"
b = "1"
Return "100".
这道题目与Add Two Numbers 这道题目类似。不同的是这道题符合我们从低位到高位的进位方式,也就是从右往左,add two numbers那道题目是从左往右,一个是十进制,一个是二进制。思路是一样的,维护一个进位carry,对于这道题目,如果当前的值大于1的时候就需要进位,此时我们就把carry设置为1。在我们扫描完字符串之后,不要忘记检测carry的值,如果为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; int carry = 0; int al = a.length() - 1; int bl = b.length() - 1; StringBuilder sb = new StringBuilder(); while(al >= 0 || bl >= 0) { int tem = 0; if(al >= 0) { tem = a.charAt(al) - '0'; } if(bl >= 0) { tem += b.charAt(bl) - '0'; } tem += carry; if(tem > 1) { carry = 1; } else { carry = 0; } sb.append(tem % 2); al --; bl --; } if(carry == 1) sb.append(carry); return sb.reverse().toString(); } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 229Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 231You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 347Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 340Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 460Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 524Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 434Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 623Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 434The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 392Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 522Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 541Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 378All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 868Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 884Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 559Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 604Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 771Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 741You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 635For a undirected graph with tre ...
相关推荐
leetCode 第67题。Swift解
python对图像提取mask部分: 代码: ...# Add binary masks to images for img_item in os.listdir(images_path): print(img_item) img_path = os.path.join(images_path, img_item) img = cv2.
Add Binary Longest Palindromic Substring Regular Expression Matching Wildcard Matching Longest Common Prefix Valid Number Integer to Roman Roman to Integer Count and Say Anagrams Valid Anagram ...
算法类,二进制加法的源代码,算法导论上的
401 | [Binary Watch](https://leetcode.com/problems/binary-watch/) | [C++](./C++/binary-watch.cpp) [Python](./Python/binary-watch.py) | _O(1)_ | _O(1)_ | Easy | | 411 | [Minimum Unique Word ...
pod repo add binary_spec git@github.com:caixiang305621856/open_binary_spec.git 2.安装mongodb # 进入 /usr/local cd /usr/local # 下载 sudo curl -O ...
Introduction ============ This is a class to calculate histogram of LBP (local binary patterns) from an input image, histograms of...* Add the calculation of histogram of uniform rotation invariant VLBP.
两个n位二进制数分别存储在两个n元数组A和B中,这两个整数的和存在一个n+1元的数组C中 答: 此问题主要是考察相加进位的问题,元素1+1 =0 并且往前进一位 ADD-BINARY(A,B) ...function addBinary($A
题目实现一个二进制加法,输入输出均为二进制字符串function binaryAdd(num1: string, num2: string): string {
leetcode ...Binary Linked-list 002 Add Two Numbers Stack 020 Valid Parenthesis Hash Table 001 TwoSum Reference 完整的学习流程 How to be a softwair engineer: 其他人详解 Python的各式演算法
67.二进制求和 (Add Binary) 69.x 的平方根 (Sqrt(x)) 70.爬楼梯 (Climbing Stairs) 83.删除排序链表中的重复元素 (Remove Duplicates from Sorted List) 88.合并两个有序数组 (Merge Sorted Array) 100.相同的树 ...
04-23从Alphabet到IntegerMapping的DecryptString 2020-04-25合并两个已排序的NodeList Fibonacci 2020-04-26查找ContinuousSequence MaximunNum 2020-04-28递归多态2020-04-30 HappyNumber 2020-05-01 AddBinary ...
AddBinary:从字符串中添加两个二进制数 AddTwoNumber:从数组中添加两个数字 BSTtoDLL:将二进制搜索树转换为双链表 BTreeIterator:二叉树迭代器:有序,前序,后序 BuildBT:从数组输入构建二进制 爬楼梯:爬...
addBinary longestPalindrome maximal rectangle :dp问题,较难 largestRectangleArea 求直方图的最大面积,左右两次扫面+剪枝优化 Valid Parentheses 用栈判断括号匹配 Regular Expression Matching 递归匹配 ...
leetcode双人赛LeetCode ...AddBinary :简单的问题。 AddTwoNumbers :简单的问题。 Anagrams :简单的#hashtable问题。 BalancedBinaryTree :简单的#balance #tree问题。 BestTimetoBuyandSellStock :简单的问题。 ...
所有题目源代码:Git地址 题目 给你两个二进制字符串,返回它们的和(用二进制表示)。 输入为 非空 字符串且只包含数字 1 和 0。 示例 1: 输入: a = 11, b = 1 ... public String addBinary(String a,
此程序采用VHDL语言,完成在16位十六进制加法器的基础上将输出进行BCD码转换,实现输出是BCD码的16位二进制加法器
BinarySerializer ... AddFile ( new LinearSerializedFile ( context ) { FilePath = relativeFilePath }); 使用不同的BinaryFile类型,例如LinearSerializedFile , MemoryMappedFile和StreamF
此程序采用VHDL语言,完成在32位十六进制加法器的基础上将输出进行BCD码转换,实现输出是BCD码的32位二进制加法器
Work with Unity 5 and Unity 4.x ...– Add 3D Binary – Add 3D Computer – Add 3D Snow – Add 3D Black Hole – Add 3D Ghost Light – Add 3D Anomaly – Add 3D Shield – Add Atmosphere Fog – Add Pixelisat