Skip to content

Commit e9f7a4b

Browse files
refactor 67
1 parent a023ef6 commit e9f7a4b

File tree

2 files changed

+35
-156
lines changed

2 files changed

+35
-156
lines changed

src/main/java/com/fishercoder/solutions/_67.java

Lines changed: 26 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
/**
44
* 67. Add Binary
5+
*
56
* Given two binary strings, return their sum (also a binary string).
67
* For example,
78
* a = "11"
@@ -10,95 +11,33 @@
1011
*/
1112

1213
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+
*/
1821
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';
3330
}
34-
if (carry != 0) {
35-
sb.append(carry);
31+
if (j >= 0) {
32+
sum += b.charAt(j--) - '0';
3633
}
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();
10341
}
42+
}
10443
}
Lines changed: 9 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,21 @@
11
package com.fishercoder;
22

33
import com.fishercoder.solutions._67;
4-
5-
import org.junit.Before;
64
import org.junit.BeforeClass;
75
import org.junit.Test;
86

97
import static junit.framework.Assert.assertEquals;
108

11-
/**
12-
* Created by fishercoder on 1/8/17.
13-
*/
149
public class _67Test {
15-
private static _67 test;
16-
private static String expected;
17-
private static String actual;
18-
private static String a;
19-
private static String b;
20-
21-
@BeforeClass
22-
public static void setup() {
23-
test = new _67();
24-
expected = new String();
25-
actual = new String();
26-
a = new String();
27-
b = new String();
28-
}
29-
30-
@Before
31-
public void setupForEachTest() {
32-
expected = "";
33-
actual = "";
34-
a = "";
35-
b = "";
36-
}
37-
38-
@Test
39-
public void test1() {
40-
41-
a = "0";
42-
b = "0";
43-
expected = "0";
44-
actual = test.addBinary(a, b);
45-
assertEquals(expected, actual);
46-
47-
}
48-
49-
@Test
50-
public void test2() {
51-
52-
a = "11";
53-
b = "1";
54-
expected = "100";
55-
actual = test.addBinary(a, b);
56-
assertEquals(expected, actual);
57-
58-
}
59-
60-
@Test
61-
public void test3() {
62-
63-
a = "100";
64-
b = "110010";
65-
expected = "110110";
66-
actual = test.addBinary(a, b);
67-
assertEquals(expected, actual);
68-
69-
}
70-
71-
@Test
72-
public void test4() {
10+
private static _67.Solution1 solution1;
7311

74-
a = "101111";
75-
b = "10";
76-
expected = "110001";
77-
actual = test.addBinary(a, b);
78-
assertEquals(expected, actual);
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _67.Solution1();
15+
}
7916

80-
}
17+
@Test
18+
public void test1() {
19+
assertEquals("100", solution1.addBinary("11", "1"));
20+
}
8121
}

0 commit comments

Comments
 (0)