LeetCode 67 : Add Binary (Java)
解題思路:從末尾依次對應相加來求,轉成int中的相加會使程式碼比較簡潔,因為可以用/和%來分別求出進位和當前往結果中新增的位。然後,把較長的字串剩下的部分和進位相加放入結果。最後,還要判斷進位此時是否為1,如果為1,則需要再往結果中新增1,否則返回結果。
public class Solution {
public String addBinary(String a, String b) {
if(a.length() < b.length()){
String tmp = a;
a = b;
b = tmp;
}
int pa = a.length()-1;
int pb = b.length()-1;
int carries = 0;
String rst = "";
while(pb >= 0){
int sum = (int)(a.charAt(pa) - '0') + (int)(b.charAt(pb) - '0') + carries;
rst = String.valueOf(sum % 2) + rst;
carries = sum / 2;
pa --;
pb --;
}
while (pa >= 0){
int sum = (int)(a.charAt(pa) - '0') + carries;
rst = String.valueOf(sum % 2) + rst;
carries = sum / 2;
pa --;
}
if (carries == 1)
rst = "1" + rst;
return rst;
}
}
相關推薦
LeetCode 67 : Add Binary (Java)
解題思路:從末尾依次對應相加來求,轉成int中的相加會使程式碼比較簡潔,因為可以用/和%來分別求出進位和當前往結果中新增的位。然後,把較長的字串剩下的部分和進位相加放入結果。最後,還要判斷進位此時是否為1,如果為1,則需要再往結果中新增1,否則返回結果。 pu
leetcode解題之67 # Add Binary Java版
67. Add Binary Given two binary strings, return their sum (also a binary string). For example, a
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#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.Add Binary (二進位制求和)
題目描述: 給定兩個二進位制字串,返回他們的和(用二進位制表示)。 輸入為非空字串且只包含數字 1 和 0。 示例 1: 輸入: a = "11", b = "1" 輸出: "100" 示例 2: 輸入: a = "1010", b = "1011" 輸出:
LeetCode---67. Add Binary
LeetCode—67. Add Binary 題目 思路及解法 要注意兩點: 1.兩個字串有可能不是等長的,這樣在相加時就可能出現空指標的問題,所以首先要把較短的字串左端補零,將兩個字串補位等長。 2.字串charAt操作是從字串的最左端開始操作的,也就是二
LeetCode--67. Add Binary
題目連結:https://leetcode.com/problems/add-binary/ 這個就是要求將兩個二進位制表示的字串相加返回一個二進位制字串,最需要注意的是進位的問題。 因為是easy的題目,所以思路比較多。 思路一:將兩個二進位制字串轉為對應的十進位制整數,然後相加,將計
LeetCode 67 -Add Binary
67. Add Binary Given two binary strings, return their sum (also a binary string). The input strings are both non-empty and contains only
leetcode 67 Add Binary
Add Binary Total Accepted: 46815 Total Submissions: 189215 My Submissions Given two binary
LeetCode 67 Add Binary(二進位制相加)(*)
翻譯 給定兩個二進位制字串,返回它們的和(也是二進位制字串)。 例如, a = "11" b = "1" 返回 "100". 原文 Given two binary strings, ret
Leetcode 67. Add Binary 二進位制加法 解題報告
1 解題思想 給定以字串為形式表達的一個二進位制數,需要求加法後的值。 這道題是一個弱化版的高精度加法,整體來說和之前做題的方式很類似,注意加法結果可能比原來長一位 2 原題 Given two binary strings, return their
Leetcode 67 Add Binary 二進位制加
題目描述:Given two binary strings, return their sum (also a binary string).For example,a = "11"b = "1"Return "100".用兩個string表示大數,返回他們的和(也用stri
leetcode 67 Add Binary(二進位制字串相加)
題目要求 給定兩個二進位制字串,返回其相加之後的和。 輸入字串都是非空的,只包含字元1或0。 解題思路 以前我們做過一道題,給數字加1:leetcode 66 plus one,其中討論了兩種情況,同樣的可以遷移到本題上,不同的是,這次我們處理的是二進位制,字串。 (1)首先