- 浏览: 18985 次
- 性别:
- 来自: 北京
文章分类
最新评论
题目描述
Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100".
解题思路
本题考查对字符串和容器的使用。主要思想是将a,b转化为列表处理,然后从低位到高位一次进行进位操作。
相关知识点
(1)对一个容器进行逆序操作
自己的代码
Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100".
解题思路
本题考查对字符串和容器的使用。主要思想是将a,b转化为列表处理,然后从低位到高位一次进行进位操作。
相关知识点
(1)对一个容器进行逆序操作
Collections.reverse(list);
自己的代码
package leetcode; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class AddBinary { public String addBinary(String a, String b) { if(a == null || b == null) return null; if(a.length() == 0) return b; if(b.length() == 0) return a; //较短为b,较长的为a if(a.length() < b.length()) { String temp = a; a = b; b = temp; } List<Integer> aList = new ArrayList<Integer>(); for(int i = a.length() - 1; i >= 0; i--) aList.add((a.charAt(i)=='0') ? 0 : 1); List<Integer> bList = new ArrayList<Integer>(); for(int i = b.length() - 1; i >= 0; i--) bList.add((b.charAt(i)=='0') ? 0 : 1); for(int i = 0; i < a.length()-b.length(); i++) bList.add(0); int carry = 0; for(int i = 0; i < aList.size(); i++){ int result = aList.get(i) + bList.get(i) + carry; if(result > 1){ carry = 1; if(result == 2) aList.set(i, 0); if(result == 3) aList.set(i, 1); } else{ carry = 0; aList.set(i, result); } } if(carry != 0) aList.add(1); Collections.reverse(aList); String s = ""; for(int i : aList) s += i; return s; } public static void main(String[] args) { String a = "11"; String b = "1"; AddBinary ab = new AddBinary(); System.out.println(ab.addBinary(a, b)); } }
发表评论
-
Java中String与StringBuffer的区别
2014-10-29 21:07 297String和StringBuffer的区别,网上资料可以说 ... -
String to Integer (atoi)
2014-10-29 17:13 396题目描述 Implement atoi to convert ... -
Implement strStr()
2014-10-28 15:17 282题目描述 Implement strStr(). Retu ... -
Valid Palindrome
2014-10-23 10:32 417题目描述 Given a string, determine ... -
ZigZag Conversion
2014-10-22 19:51 340题目描述 The string "PAYPALIS ... -
Longest Common Prefix
2014-10-22 19:44 326题目描述 Write a function to find t ... -
Count and Say
2014-10-22 19:44 345题目描述 The count-and-say sequence ... -
Valid Sudoku
2014-10-21 10:22 352题目描述 Determine if a Sudoku is v ... -
Valid Parentheses
2014-10-21 09:41 321题目描述 Given a string containing ... -
Palindrome Number
2014-10-21 09:41 343题目描述 Determine whether an integ ... -
Length of Last Word
2014-10-21 09:41 354题目描述 Given a string s consists ... -
Minimum Depth of Binary Tree
2014-10-21 09:41 305题目描述 Given a binary tree, find ... -
Remove Nth Node From End of List
2014-10-20 16:36 254题目描述 Given a linked list, remov ... -
Path Sum
2014-10-20 15:37 291题目描述 Given a binary tree and a ... -
Binary Tree Level Order Traversal II
2014-10-20 11:17 230题目描述 Given a binary tree, retur ... -
Binary Tree Level Order Traversal
2014-10-20 11:03 289题目描述 Given a binary tree, retur ... -
Pascal's Triangle II
2014-10-20 10:07 254题目描述 Given an index k, return t ... -
Pascal's Triangle
2014-10-19 12:24 316题目描述 Given numRows, generate th ... -
Plus One
2014-10-19 11:51 333题目描述 Given a non-negative numbe ... -
Merge Sorted Array
2014-10-18 10:45 396题目描述 Given two sorted integer a ...
相关推荐
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 ...
两个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,
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.
此程序采用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