1
1
import java .util .Arrays ;
2
2
3
- // // "101" "010" -> "111"
4
- // // "101" "011" -> "1000"
5
-
3
+ /**
4
+ *
5
+ * Location https://coderpad.io/WGT7MX76
6
+ *
7
+ * Example showing how to work with two arrays which need
8
+ * to be aligned at their ends (not starts), and where the
9
+ * result of the current might affect the next.
10
+ *
11
+ * Useful for:
12
+ * - binary arithmetic ("101" "010" -> "111" OR "101" "011" -> "1000")
13
+ * - iterating through arrays of json where the following item might affect the result of the previous (a carry behavior)
14
+ */
6
15
public class BinaryArithmetic {
7
16
8
17
/**
9
18
* minimal time is going to be max length of one of the string
10
- * x2 because of initialization and conversion to string
19
+ * x3 because of
20
+ * - initialization and
21
+ * - actual computation loop
22
+ * - conversion to string
11
23
*/
12
24
static String addBinaryStrings (String a , String b ) throws Exception {
13
25
if (a == null ) {
@@ -68,8 +80,6 @@ static String addBinaryStrings(String a, String b) throws Exception {
68
80
}
69
81
}
70
82
71
- // previousResult = current;
72
- // Convert array of ints into its string value
73
83
String asString = "" ;
74
84
if (previousResult == 1 ) {
75
85
asString += "1" ;
@@ -95,15 +105,4 @@ static int addChar(char a, char b) throws Exception {
95
105
return 0 ;
96
106
}
97
107
98
-
99
-
100
- // // something(current, positionofcarry){
101
-
102
- // // if(pofsitionofcarry!= null){
103
- // // done
104
- // // }
105
- // // something(positionofcarry, psotionofcarry--)
106
-
107
- // // }
108
- //
109
108
}
0 commit comments