FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
coding-Interview/DecodeString.java at master · arjunmullick/coding-Interview · GitHub
arjunmullick
/
coding-Interview
Public
Notifications
You must be signed in to change notification settings
Fork
1
Star
3
Code
Issues
0
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
coding-Interview
/
DecodeString.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
41 lines (35 loc) · 1.33 KB
Breadcrumbs
coding-Interview
/
DecodeString.java
Copy path
File metadata and controls
41 lines (35 loc) · 1.33 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
public
class
DecodeString
{
//https://leetcode.com/problems/decode-string/
class
Solution
{
public
String
decodeString
(
String
s
) {
Deque
<
Integer
>
numStack
=
new
ArrayDeque
<>();
Deque
<
String
>
strStack
=
new
ArrayDeque
<>();
StringBuilder
tail
=
new
StringBuilder
();
int
n
=
s
.
length
();
for
(
int
i
=
0
;
i
<
n
;
i
++) {
char
c
=
s
.
charAt
(
i
);
if
(
Character
.
isDigit
(
c
)) {
int
num
=
c
-
'0'
;
while
(
i
+
1
<
n
&&
Character
.
isDigit
(
s
.
charAt
(
i
+
1
))) {
num
=
num
*
10
+
s
.
charAt
(
i
+
1
) -
'0'
;
i
++;
}
numStack
.
push
(
num
);
}
else
if
(
c
==
'['
) {
strStack
.
push
(
tail
.
toString
());
tail
=
new
StringBuilder
();
}
else
if
(
c
==
']'
) {
StringBuilder
tmp
=
new
StringBuilder
(
strStack
.
pop
());
int
repeatedTimes
=
numStack
.
pop
();
for
(
int
j
=
0
;
j
<
repeatedTimes
;
j
++) {
tmp
.
append
(
tail
);
}
tail
=
tmp
;
}
else
{
tail
.
append
(
c
);
}
}
return
tail
.
toString
();
}
}
}
Back
|
FazBrowse Home
|
New Git URL