Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static Stack<Integer> one(Queue<Integer> q1, Queue<Integer> q2)
- {
- Stack<Integer> s1 = new Stack<Integer>();
- Stack<Integer> s2 = new Stack<Integer>();
- Stack<Integer> res = new Stack<Integer>();
- int temp=0, sum;
- while(!q1.isEmpty())
- s1.push(q1.remove());
- while(!q2.isEmpty())
- s2.push(q2.remove());
- while(!s1.isEmpty() && !s2.isEmpty()) // <---THE QUESTION
- {
- sum = s1.pop()+s2.pop()+temp;
- if(sum<10)
- {
- res.push(sum);
- temp = 0;
- }
- else
- {
- res.push(sum%10);
- temp=sum/10;
- }
- }
- while(!s1.isEmpty())
- {
- sum=s1.pop()+temp;
- res.push(sum);
- temp=0;
- }
- while(!s2.isEmpty())
- {
- sum=s2.pop()+temp;
- res.push(sum);
- temp=0;
- }
- return res;
- }
Advertisement
Add Comment
Please, Sign In to add comment