|
2 | 2 |
|
3 | 3 | /**
|
4 | 4 | * 67. Add Binary
|
| 5 | + * |
5 | 6 | * Given two binary strings, return their sum (also a binary string).
|
6 | 7 | * For example,
|
7 | 8 | * a = "11"
|
|
10 | 11 | */
|
11 | 12 |
|
12 | 13 | public class _67 {
|
13 |
| - //then I turned to Discuss, this post is concise: https://discuss.leetcode.com/topic/13698/short-ac-solution-in-java-with-explanation |
14 |
| - //Tricks and things learned that could be learned: |
15 |
| - //1. use StringBuilder.reverse() function! Nice! |
16 |
| - //2. if a numeric number is represented/stored in String, how to get its value: use Character.getNumericValue(s.charAt(i)) |
17 |
| - //3. directly adding/subtracting chars will end up working with their ASCII numbers, e.g. chars[0] = 'a', chars[1] = 'b', then chars[0] + chars[1] will become 195 |
| 14 | + public static class Solution1 { |
| 15 | + /** |
| 16 | + * credit: https://discuss.leetcode.com/topic/13698/short-ac-solution-in-java-with-explanation |
| 17 | + * 1. use StringBuilder.reverse() function! Nice! |
| 18 | + * 2. if a numeric number is represented/stored in String, how to get its value: use Character.getNumericValue(s.charAt(i)) |
| 19 | + * 3. directly adding/subtracting chars will end up working with their ASCII numbers, e.g. chars[0] = 'a', chars[1] = 'b', then chars[0] + chars[1] will become 195. |
| 20 | + */ |
18 | 21 | public String addBinary(String a, String b) {
|
19 |
| - int carry = 0; |
20 |
| - int i = a.length() - 1; |
21 |
| - int j = b.length() - 1; |
22 |
| - StringBuilder sb = new StringBuilder(); |
23 |
| - while (i >= 0 || j >= 0) { |
24 |
| - int sum = carry; |
25 |
| - if (i >= 0) { |
26 |
| - sum += a.charAt(i--) - '0'; |
27 |
| - } |
28 |
| - if (j >= 0) { |
29 |
| - sum += b.charAt(j--) - '0'; |
30 |
| - } |
31 |
| - sb.append(sum % 2); |
32 |
| - carry = sum / 2; |
| 22 | + int carry = 0; |
| 23 | + int i = a.length() - 1; |
| 24 | + int j = b.length() - 1; |
| 25 | + StringBuilder sb = new StringBuilder(); |
| 26 | + while (i >= 0 || j >= 0) { |
| 27 | + int sum = carry; |
| 28 | + if (i >= 0) { |
| 29 | + sum += a.charAt(i--) - '0'; |
33 | 30 | }
|
34 |
| - if (carry != 0) { |
35 |
| - sb.append(carry); |
| 31 | + if (j >= 0) { |
| 32 | + sum += b.charAt(j--) - '0'; |
36 | 33 | }
|
37 |
| - return sb.reverse().toString(); |
38 |
| - } |
39 |
| - |
40 |
| - //my original lengthy but AC'ed solution |
41 |
| - public String addBinary_my_original_accepted_but_lengthy_solution(String a, String b) { |
42 |
| - char[] longer = (a.length() >= b.length()) ? a.toCharArray() : b.toCharArray(); |
43 |
| - char[] shorter = (a.length() < b.length()) ? a.toCharArray() : b.toCharArray(); |
44 |
| - //at the maximum, the result length will be Math.max(a.length, b.length)+1; |
45 |
| - //let's use Math.max() as the length first, if the most signifant bits add up to a carry, then we'll add one more bit |
46 |
| - char[] result = new char[longer.length]; |
47 |
| - boolean carry = false; |
48 |
| - int i = longer.length - 1; |
49 |
| - int j = shorter.length - 1; |
50 |
| - System.out.println(Character.getNumericValue(longer[i]) + Character.getNumericValue(shorter[j])); |
51 |
| - System.out.println((int) longer[i] + (int) shorter[j]); |
52 |
| - System.out.println(longer[i] + shorter[j]); |
53 |
| - System.out.println('a' + 'b'); |
54 |
| - for (; i >= 0 || j >= 0; i--, j--) { |
55 |
| - if (j < 0 && i >= 0) { |
56 |
| - if (carry) { |
57 |
| - if (Character.getNumericValue(longer[i]) + 1 == 2) { |
58 |
| - result[i] = '0'; |
59 |
| - carry = true; |
60 |
| - } else { |
61 |
| - result[i] = '1'; |
62 |
| - carry = false; |
63 |
| - } |
64 |
| - } else { |
65 |
| - for (int k = i; k >= 0; k--) { |
66 |
| - result[k] = longer[k]; |
67 |
| - } |
68 |
| - return new String(result); |
69 |
| - } |
70 |
| - } else if (Character.getNumericValue(longer[i]) + Character.getNumericValue(shorter[j]) == 2) { |
71 |
| - if (carry) { |
72 |
| - result[i] = '1'; |
73 |
| - } else { |
74 |
| - result[i] = '0'; |
75 |
| - } |
76 |
| - carry = true; |
77 |
| - } else if (Character.getNumericValue(longer[i]) + Character.getNumericValue(shorter[j]) == 1) { |
78 |
| - if (carry) { |
79 |
| - result[i] = '0'; |
80 |
| - carry = true; |
81 |
| - } else { |
82 |
| - result[i] = '1'; |
83 |
| - carry = false; |
84 |
| - } |
85 |
| - } else if (Character.getNumericValue(longer[i]) + Character.getNumericValue(shorter[j]) == 0) { |
86 |
| - if (carry) { |
87 |
| - result[i] = '1'; |
88 |
| - } else { |
89 |
| - result[i] = '0'; |
90 |
| - } |
91 |
| - carry = false; |
92 |
| - } |
93 |
| - } |
94 |
| - if (carry) { |
95 |
| - char[] newResult = new char[longer.length + 1]; |
96 |
| - newResult[0] = '1'; |
97 |
| - for (int k = 0; k < result.length; k++) { |
98 |
| - newResult[k + 1] = result[k]; |
99 |
| - } |
100 |
| - return new String(newResult); |
101 |
| - } |
102 |
| - return new String(result); |
| 34 | + sb.append(sum % 2); |
| 35 | + carry = sum / 2; |
| 36 | + } |
| 37 | + if (carry != 0) { |
| 38 | + sb.append(carry); |
| 39 | + } |
| 40 | + return sb.reverse().toString(); |
103 | 41 | }
|
| 42 | + } |
104 | 43 | }
|
0 commit comments