-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path394.php
54 lines (53 loc) · 1.46 KB
/
394.php
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
<?php
class Solution
{
/**
* @param String $s
* @return String
*/
public function decodeString($s)
{
$stack = [];
$ans = "";
$repeat = 0;
for ($i = 0; $i < strlen($s); $i++) {
if ($s[$i] != ']') {
if (is_numeric($s[$i])) {
$repeat = $repeat * 10 + $s[$i];
} else {
if ($repeat != 0) {
array_push($stack, $repeat);
$repeat = 0;
}
array_push($stack, $s[$i]);
}
} else {
$temp = "";
$data = "";
$str = "";
while ($data != '[') {
$data = array_pop($stack);
if ($data != '[') {
$str = $data . $str;
}
}
$repeat = array_pop($stack);
for ($j = 0; $j < $repeat; $j++) {
$temp .= $str;
}
if (!empty($stack)) {
for ($k = 0; $k < strlen($temp); $k++) {
array_push($stack, $temp[$k]);
}
} else {
$ans .= $temp;
}
$repeat = 0;
}
}
while (!empty($stack)) {
$ans .= array_shift($stack);
}
return $ans;
}
}