Given two binary strings, return their sum (also a binary string). For example, a ="11" b ="1" Return"100". 解题思路1:判断当前字符所表示的数字,产生输出和进位。缺点:程序比较复杂。 代码1: class Solution { public: string addBinary(string a, string b) { char carry='0'; string c; ...
publicclassSolution {publicString addBinary(String a, String b) {if(a.equals("") && b.equals(""))return"0";if(a ==null|| a.equals("") || a.equals("0"))returnb;if(b ==null|| b.equals("") || b.equals("0"))returna;char[] a1 =a.toCharArray();char[] b1 =b.toCharA...
usestd::iter; implSolution{ pubfnadd_binary(a:String,b:String)->String{ //进位,初始为0 letmutcarry=0; //收集a和b按位加的结果 letmutresult=a //返回底层的字节切片 .as_bytes() //转换成迭代器 .iter() //提前反向迭代(后面会加上无限的'0',所以不能后面同时反向) .rev() //在a后面...
leetcode -- Add Binary -- 简单要了解 https://leetcode.com/problems/add-binary/ 知道二进制加法原理即可,这里只需要知道进位是除数,余数是结果就行。最后不要忽略reg里面的值 class Solution(object): def addBinary(self, a, b): """ :type a: str :type b: str :rtype: str """ if not a:...
class Solution { public: string addBinary(string a, string b) { string s; s.reserve(a.size() + b.size()); int c = 0, i = a.size() - 1, j = b.size() - 1; while(i >= 0 || j >= 0 || c == 1) { c += i >= 0 ? a[i--] - '0' : 0; ...
Leetcode - Add Binary Paste_Image.png My code: public class Solution { public String addBinary(String a, String b) { if (a == null || a.length() == 0 || b == null || b.length() == 0) return null; int len = Math.max(a.length(), b.length()) + 1;...
Solution: classSolution:defaddBinary(self,a:str,b:str)->str:result=""remainder=0defextract(string,index):ifindex<-len(string):return0else:returnint(string[index])foriinrange(-1,-max(len(a),len(b))-2,-1):add=extract(a,i)+extract(b,i)+remainder ...
- [930. 和相同的二元子数组](https://leetcode.cn/problems/binary-subarrays-with-sum/) 1592 - [1248. 统计「优美子数组」](https://leetcode.cn/problems/count-number-of-nice-subarrays/) 1624 - [1712. 将数组分成三个子数组的方案数](https://leetcode.cn/problems/ways-to-split-array-into...
* ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) { // Start typing your C/C++ solution below if(l1 == NULL && l2 == NULL) { return NULL; ...
🔥 Join LeetCode to Code! View your Submission records hereRegister or Sign In Ln 1, Col 1 You need to Login / Sign up to run or submit Case 1Case 2 a = "11" b = "1" 1 2 3 4 "11" "1" "1010" "1011" Source