[LeetCode] Add Binary
Add Binary
Given two binary strings, return their sum (also a binary string).
For example, a = “11” b = “1”
Return “100”.
題目的意思也是比較簡單,兩個二進位制陣列相加,如果兩個陣列都已經加完還有進位的話,則要在最前面加’1’
class Solution {
public:
string addBinary(string a, string b) {
int m = a.size()-1;
int n = b.size()-1;
if(n>m) return addBinary(b,a);
int carry = 0;
int dig;
while(m>=0){
dig = a[m]-'0'+carry;
if(n>=0){
dig += (b[n]-'0');
--n;
}
a[m]=dig%2+'0';
carry=dig/2;
--m;
}
if (carry)
a.insert(0,1,'1'); //string.insert(pos,n.c);string.insert(pos,string);
return a;
}
};
相關推薦
[leetcode]Add Binary
Given two binary strings, return their sum (also a binary string). The input strings are both non-empty and contains only characters
leetcode add binary
leet code add binary 題目:https://leetcode.com/problems/add-binary/ 解題思路: 1.獲取兩個字串長度的最大值 2.記錄進位標記 int carry 3.從後向前,以此取數,把與2取模的值插入字串中
LeetCode-Add Binary
Description: Given two binary strings, return their sum (also a binary string). The input strings a
LeetCode Add Binary
Problem Given two binary strings, return their sum (also a binary string). For example, a = “11” b = “1” Return “100”. 題
二進位制字串求和-leetcode Add Binary
題目描述 知識點及思路 總結 一.題目描述 簡述:兩個字串求和 二.知識點及思路 2.1知識點:二進位制進位;超前加法器;與或操作 2.2思路:①將字串對齊②用flag表徵進位標誌③逐位相加
leetcode [Add Binary]
import java.util.Vector; class Solution { public String addBinary(String a, String b) { //
[LeetCode] Add Binary
Add Binary Given two binary strings, return their sum (also a binary string). For examp
LeetCode | Add Binary(二進位制相加)
Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". 題目解析: 對string瞭解不熟練,本來想著
Leetcode:Add Binary 二進位制相加
Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". 思路同十進位制的大數相加。程式碼如下: cla
LeetCode 67 Add Binary(二進制相加)(*)
ron string class ide 字符串 字符 dbi binary 目的 翻譯 給定兩個二進制字符串,返回它們的和(也是二進制字符串)。 比如, a = "11" b = "1" 返回 "100". 原文 Given two bin
LeetCode 67. Add Binary
tco names isp style res cli one result reverse https://leetcode.com/problems/add-binary/description/ Given two binary strings, return th
LeetCode 67. Add Binary (二進制相加)
-i har 相加 .cn cheng code tostring list 還要 Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"
[LeetCode] 67. Add Binary 二進制數相加
strings pre bin ons dbi light 反向輸出 brush AD Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "
LeetCode - 67. Add Binary(4ms)
div ins code nbsp The bsp als return example Given two binary strings, return their sum (also a binary string). The input strings are bot
[leetcode]67.Add Binary
ngs turn 結果 一個 += return 兩個 har ddb 題目 Given two binary strings, return their sum (also a binary string). The input strings are both non-
[leetcode]67. Add Binary 二進制加法
out har strings empty add str leetcode lee binary Given two binary strings, return their sum (also a binary string). The input strings ar
[LeetCode] 67. Add Binary
題:https://leetcode.com/problems/add-binary/description/ 題目 Given two binary strings, return their sum (also a binary string). The input stri
LeetCode演算法題-Add Binary(Java實現)
這是悅樂書的第157次更新,第159篇原創 01 看題和準備 今天介紹的是LeetCode演算法題中Easy級別的第16題(順位題號是67)。給定兩個二進位制字串,返回它們的總和(也是二進位制字串)。輸入字串都是非空的,只包含字元1或0。 例如: 輸入:a =“11”
LeetCode#67: Add Binary
Description Given two binary strings, return their sum (also a binary string). The input strings are both non-empty and contains only charac
【leetcode】67.(Easy)Add Binary
提交程式碼: class Solution { public String addBinary(String a, String b) { int numA,numB,carry=0; int p1=a.length()-1,p2=b.length()-1;