Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit d3ba61a

Browse files
committed
update
1 parent b272674 commit d3ba61a

File tree

4 files changed

+125
-27
lines changed

4 files changed

+125
-27
lines changed

DataAndAlgoL/Chp3LinkedLists/Problem44.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,30 @@ public static void main(String[] args) {
1212

1313
}
1414

15-
public ListNode getJosephusPosition(int N, int M){
16-
ListNode p;
17-
ListNode q;
18-
//create circular linked list containing all the players
19-
p.setData(1);
15+
// public ListNode getJosephusPosition(int N, int M){
16+
// ListNode p;
17+
// ListNode q;
18+
// //create circular linked list containing all the players
19+
// p.setData(1);
2020

21-
q=p;
21+
// q=p;
2222

23-
for(int i=2; i<= N; i++){
24-
p=p.getNext();
25-
p.setData(i);
26-
}
23+
// for(int i=2; i<= N; i++){
24+
// p=p.getNext();
25+
// p.setData(i);
26+
// }
2727

28-
p.setNext(q); // Close the circular linked list by having the last node point to the first
28+
// p.setNext(q); // Close the circular linked list by having the last node point to the first
2929

30-
for(int count=N; count >1; count--){
31-
for(int i=0; i<M-1; i++){
32-
p=p.getNext();
33-
p.setNext(p.getNext().getNext());// remove the eliminated player from the list
34-
}
35-
}
30+
// for(int count=N; count >1; count--){
31+
// for(int i=0; i<M-1; i++){
32+
// p=p.getNext();
33+
// p.setNext(p.getNext().getNext());// remove the eliminated player from the list
34+
// }
35+
// }
3636

37-
return p;
37+
// return p;
3838

3939

40-
}
40+
// }
4141
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package DataAndAlgoL.Chpt4Stacks;
2+
3+
import java.util.Stack;
4+
5+
public class postFixEval {
6+
public static void main(String[] args) {
7+
8+
}
9+
10+
public static int postFix(String[] tokens){
11+
Stack<Integer> stack= new Stack<>();
12+
13+
for(String token: tokens){
14+
if(token.equals("*")){
15+
int res= stack.pop() * stack.pop();
16+
stack.push(res);
17+
}else if(token.equals("/")){
18+
int res=stack.pop() / stack.pop();
19+
stack.push(res);
20+
}else if(token.equals("+")){
21+
int res= stack.pop() + stack.pop();
22+
stack.push(res);
23+
}else if(token.equals("-")){
24+
int res= stack.pop() - stack.pop();
25+
stack.push(res);
26+
}else{
27+
stack.push(Integer.parseInt(token));
28+
}
29+
}
30+
31+
return stack.pop();
32+
}
33+
}

LeetCode/Grind169/Stacks/BackSpaceStringCompare_844.java

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,45 @@
55

66
public class BackSpaceStringCompare_844 {
77
public static void main(String[] args) {
8-
Scanner kbr= new Scanner(System.in);
9-
System.out.println("Type 1st string: ");
10-
String s = kbr.nextLine();
11-
System.out.println("Enter 2nd String: ");
12-
String t= kbr.nextLine();
8+
try (Scanner kbr = new Scanner(System.in)) {
9+
System.out.println("Type 1st string: ");
10+
String s = kbr.nextLine();
11+
System.out.println("Enter 2nd String: ");
12+
String t= kbr.nextLine();
1313

14-
boolean check= backspaceCompare(s, t);
15-
System.out.println(check);
14+
boolean check= backspaceCompare(s, t);
15+
System.out.println(check);
16+
}
1617
}
1718

1819
public static boolean backspaceCompare(String s, String t) {
19-
20+
//Maintain 2 stacks for each string which contain the final resultant string
21+
Stack<Character> stack1= new Stack<>();
22+
Stack<Character> stack2= new Stack<>();
23+
24+
//iterate character by character and put them in stacks
25+
for(char ch: s.toCharArray()){
26+
if(ch == '#'){
27+
if(!stack1.isEmpty()){
28+
stack1.pop();
29+
}
30+
continue;
31+
}
32+
stack1.push(ch);
33+
}
34+
35+
for(char cl: t.toCharArray()){
36+
if(cl == '#'){
37+
if(!stack2.isEmpty()){
38+
stack2.pop();
39+
}
40+
continue;
41+
}
42+
stack2.push(cl);
43+
}
44+
45+
//use stack JAVA ADT comparison method to compare both stacks and return boolean
46+
return stack1.equals(stack2);
47+
2048
}
2149
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package Grind169.Stacks;
2+
3+
import java.util.Stack;
4+
5+
public class ReversePolishNotation_150 {
6+
public static void main(String[] args) {
7+
8+
}
9+
10+
public static int evalRPN(String[] tokens) {
11+
//When we have an operator we pop the last 2 numbers and add them to the stack again.
12+
int a,b;
13+
Stack<Integer> S = new Stack<Integer>();
14+
for (String s : tokens) {
15+
if(s.equals("+")) {
16+
S.add(S.pop()+S.pop());
17+
}
18+
else if(s.equals("/")) {
19+
b = S.pop();
20+
a = S.pop();
21+
S.add(a / b);
22+
}
23+
else if(s.equals("*")) {
24+
S.add(S.pop() * S.pop());
25+
}
26+
else if(s.equals("-")) {
27+
b = S.pop();
28+
a = S.pop();
29+
S.add(a - b);
30+
}
31+
else {
32+
S.add(Integer.parseInt(s));
33+
}
34+
}
35+
return S.pop();
36+
}
37+
}

0 commit comments

Comments
 (0)