FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode/problems/src/stack/BasicCalculator.java at master · KindleBooks66/leetcode · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
KindleBooks66
/
leetcode
Public
forked from
gouthampradhan/leetcode
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
leetcode
/
problems
/
src
/
stack
/
BasicCalculator.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
84 lines (80 loc) · 2.48 KB
Breadcrumbs
leetcode
/
problems
/
src
/
stack
/
BasicCalculator.java
Copy path
File metadata and controls
84 lines (80 loc) · 2.48 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package
stack
;
import
java
.
util
.
Stack
;
/**
* Created by gouthamvidyapradhan on 06/02/2018. Implement a basic calculator to evaluate a simple
* expression string.
*
* <p>The expression string may contain open ( and closing parentheses ), the plus + or minus sign
* -, non-negative integers and empty spaces .
*
* <p>You may assume that the given expression is always valid.
*
* <p>Some examples: "1 + 1" = 2 " 2-1 + 2 " = 3 "(1+(4+5+2)-3)+(6+8)" = 23 Note: Do not use the
* eval built-in library function.
*
* <p>Solution: O(n) where n is the length of the string. Maintain a stack and push each character
* from the string (ignore space). As soon as a close parentheses ')' is encountered, start to pop
* values and sum-up the total until '(' is poped. Push the total back to stack and continue to
* iterate. The final result will be in the top of the stack which is the last and only element in
* stack.
*/
public
class
BasicCalculator
{
/**
* Main method
*
* @param args
* @throws Exception
*/
public
static
void
main
(
String
[]
args
)
throws
Exception
{
System
.
out
.
println
(
new
BasicCalculator
().
calculate
(
"2-1 + (2 - 3) - ((2 - (2 - (3 - (4 - 5)))))"
));
}
public
int
calculate
(
String
s
) {
Stack
<
String
>
stack
=
new
Stack
<>();
String
num
=
""
;
s
=
"("
+
s
+
")"
;
for
(
char
c
:
s
.
toCharArray
()) {
switch
(
c
) {
case
' '
:
case
'('
:
case
'+'
:
case
'-'
:
if
(!
num
.
equals
(
""
)) {
stack
.
push
(
String
.
valueOf
(
num
));
num
=
""
;
}
if
(
c
!=
' '
) {
// ignore blank
stack
.
push
(
String
.
valueOf
(
c
));
}
break
;
case
')'
:
if
(!
num
.
equals
(
""
)) {
stack
.
push
(
String
.
valueOf
(
num
));
num
=
""
;
}
int
sum
=
0
;
int
prev
=
0
;
// maintain a prev value inorder to handle minus '-'
while
(!
stack
.
isEmpty
()) {
String
top
=
stack
.
pop
();
if
(
top
.
equals
(
"-"
)) {
sum
-= (
prev
*
2
);
prev
=
0
;
}
else
if
(
top
.
equals
(
"+"
)) {
// ignore
}
else
if
(
top
.
equals
(
"("
)) {
stack
.
push
(
String
.
valueOf
(
sum
));
break
;
}
else
{
sum
+=
Integer
.
parseInt
(
top
);
prev
=
Integer
.
parseInt
(
top
);
}
}
break
;
default
:
num
+=
String
.
valueOf
(
c
);
break
;
}
}
return
Integer
.
parseInt
(
stack
.
peek
());
}
}
Back
|
FazBrowse Home
|
New Git URL