LeetCode: Add Binary 解题报告

Add Binary
Given two binary strings, return their sum (also a binary string).

For example,
a = "11"
b = "1"
Return "100".

SOLUTION:

指针指到两个字符串的末尾,不断往前推进,用carry表示进位。用stringbuilder来记录结果。

使用insert(0, c)函数将加出的结果不断插入到STRINGBUILDER.

 1 public class Solution {
 2     public String addBinary(String a, String b) {
 3         if (a == null || b == null) {
 4             return null;
 5         }
 6
 7         if (a.length() == 0) {
 8             return b;
 9         }
10
11         if (b.length() == 0) {
12             return a;
13         }
14
15         StringBuilder sb = new StringBuilder();
16
17         int p1 = a.length() - 1;
18         int p2 = b.length() - 1;
19
20         int carry = 0;
21         while (p1 >= 0 || p2 >= 0) {
22             int sum = carry;
23             if (p1 >= 0) {
24                 sum += (a.charAt(p1) - ‘0‘);
25             }
26
27             if (p2 >= 0) {
28                 sum += (b.charAt(p2) - ‘0‘);
29             }
30
31             char c = sum % 2 == 1 ? ‘1‘: ‘0‘;
32             sb.insert(0, c);
33             carry = sum / 2;
34
35             p1--;
36             p2--;
37         }
38
39         if (carry == 1) {
40             sb.insert(0, ‘1‘);
41         }
42
43         return sb.toString();
44     }
45 }

GITHUB CODE:

AddBinary.java

时间: 2024-10-01 04:09:36

LeetCode: Add Binary 解题报告的相关文章

[leetcode] 67. Add Binary 解题报告

Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 判断数字时用charAt(i)-'0'即可 public String addBinary(String a, String b) { StringBuilder sb = new StringBuilder(); int i=a.l

[leetcode]Add Binary @ Python

原题地址:https://oj.leetcode.com/problems/add-binary/ 题意: Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 解题思路:提供两种实现方式吧. 代码一: class Solution: # @param a, a string # @pa

LeetCode: Combination Sum 解题报告

Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Question Solution Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The

【LeetCode】Subsets 解题报告

[题目] Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,3], a solution is: [ [3], [1], [2], [1,2,

LeetCode——Add Binary

Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". 求数字字符串的二进制和.同之前的数组代表数字,两个数组相加一样,只不过进位变成了2.可能两个串的长度不一样,故逆转,从左到右加下去,最后再逆转. public static String addBinary(String a,

[LeetCode]LRU Cache, 解题报告

题目 Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.

[LeetCode] [Add Binary 2012-04-02 ]

Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". string 的操作,短string补位.两个"0"会输出一个"00",要特殊处理,plus如果最后为"1",要补上. ? 1 2 3 4 5 6 7 8 9 10 1

LeetCode Add Binary 结题报告

https://oj.leetcode.com/problems/add-binary/ Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". 解题报告:二进制加法,分类为:Easy.没有任何算法,就是按照普通的方法计算即可. 一开始写了一个高低位reverse的,然后再从0到高位

【LeetCode】67. Add Binary 解题小结

题目: Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". class Solution { public: string addBinary(string a, string b) { string res(a.size()+b.size(),'0'); int i = a.size