-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcomputeString.java
More file actions
153 lines (145 loc) · 3.14 KB
/
computeString.java
File metadata and controls
153 lines (145 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
* Given two string representations of binary numbers, return a string
* representation of their sum. For example:
*
* "101"
* + "11" => badd("101", "11") -> "1000"
* ------
* "1000"
*
* "11"
* + "10" => badd("11", "10") -> "101"
* ------
* "101"
*
* "01"
* + "10" => badd("01", "10") -> "011"
* ------ (leading '0' is optional)
* "011"
*
*/
import java.util.Stack;
public class computeString{
public String badd(String x, String y) {
Stack<Integer> stack1 = new Stack<Integer> ();
Stack<Integer> stack2 = new Stack<Integer> ();
Stack<Integer> stack3 = new Stack<Integer> ();
String z = "";
for(int i=0; i <x.length(); i++){
int xint = Character.getNumericValue(x.charAt(i));
stack1.push(xint);
}
for(int j=0; j <y.length(); j++){
int yint = Character.getNumericValue(y.charAt(j));
stack2.push(yint);
}
//two stack for x, y;
int x1 =0;
int x2 =0;
int remain = 0;
if (x.length()<y.length()){
while(!stack1.empty()){
x1 = stack1.pop();
x2 =stack2.pop();
if (remain ==0){
if(x1 == 0 && x2 ==0){
stack3.push(0);
}
else if (x1 ==0&& x2==1 ){
stack3.push(1);
}
else if(x1 ==1 && x2 == 0){
stack3.push(1);
}
else{
stack3.push(0);
remain = 1;
}
}
else{
if(x1 == 0 && x2 ==0){
stack3.push(1);
remain = 0;
}
else if (x1 ==0&& x2==1 ){
stack3.push(0);
remain = 1;
}
else if(x1 ==1 && x2 == 0){
stack3.push(0);
remain = 1;
}
else{
stack3.push(1);
remain = 1;
}
}
}
while(!stack2.empty()){
stack3.push(stack2.pop());
}
}
else{
while(!stack2.empty()){
x1 = stack1.pop();
x2 =stack2.pop();
if (remain ==0){
if(x1 == 0 && x2 ==0){
stack3.push(0);
}
else if (x1 ==0&& x2==1 ){
stack3.push(1);
}
else if(x1 ==1 && x2 == 0){
stack3.push(1);
}
else{
stack3.push(0);
remain = 1;
}
}
else{
if(x1 == 0 && x2 ==0){
stack3.push(1);
remain = 0;
}
else if (x1 ==0&& x2==1 ){
stack3.push(0);
remain = 1;
}
else if(x1 ==1 && x2 == 0){
stack3.push(0);
remain = 1;
}
else{
stack3.push(1);
remain = 1;
}
}
}
while(!stack1.empty()){
stack3.push(stack1.pop());
}
}
if(remain == 1){
stack3.push(remain);
}
else{stack3.push(0);}
while (!stack3.empty()){
z = z+stack3.pop();
}
return z;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Test11 test11 = new Test11();
String a = "101";
String b = "111";
String c = null;
c = test11.badd(a, b);
System.out.println(c);
}
}