Skip to content

[pull] main from doocs:main #506

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 130 additions & 1 deletion solution/0400-0499/0439.Ternary Expression Parser/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,151 @@ or "(F ? 1 : (T ? 4 : 5))" --> "(T ? 4 : 5)" --> "4"

<!-- 这里可写通用的实现逻辑 -->

**方法一:栈**

我们从右到左遍历字符串 `expression`,对于当前遍历到的字符 $c$:

- 如果 $c$ 是字符 `':'`,则跳过;
- 如果 $c$ 是字符 `'?'`,那么意味着下一个即将遍历到的字符是条件表达式的条件,我们用一个布尔变量 `cond` 标记;
- 如果 $c$ 的上一个遍历到的字符是 `'?'`,也即布尔变量 `cond` 为 `true`,那么我们判断当前字符 $c$ 是否为字符 `'T'`,如果是,那么我们要保留栈顶第一个元素,弹出栈顶第二个元素;否则,我们要保留栈顶第二个元素,弹出栈顶第一个元素。最后,将 `cond` 置为 `false`;
- 否则,我们将当前字符 $c$ 入栈。

最后,栈中只剩下一个元素,即为表达式的结果。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 `expression` 的长度。

<!-- tabs:start -->

### **Python3**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```python

class Solution:
def parseTernary(self, expression: str) -> str:
stk = []
cond = False
for c in expression[::-1]:
if c == ':':
continue
if c == '?':
cond = True
else:
if cond:
if c == 'T':
x = stk.pop()
stk.pop()
stk.append(x)
else:
stk.pop()
cond = False
else:
stk.append(c)
return stk[0]
```

### **Java**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```java
class Solution {
public String parseTernary(String expression) {
Deque<Character> stk = new ArrayDeque<>();
boolean cond = false;
for (int i = expression.length() - 1; i >= 0; --i) {
char c = expression.charAt(i);
if (c == ':') {
continue;
}
if (c == '?') {
cond = true;
} else {
if (cond) {
if (c == 'T') {
char x = stk.pop();
stk.pop();
stk.push(x);
} else {
stk.pop();
}
cond = false;
} else {
stk.push(c);
}
}
}
return String.valueOf(stk.peek());
}
}
```

### **C++**

```cpp
class Solution {
public:
string parseTernary(string expression) {
string stk;
bool cond = false;
reverse(expression.begin(), expression.end());
for (char& c : expression) {
if (c == ':') {
continue;
}
if (c == '?') {
cond = true;
} else {
if (cond) {
if (c == 'T') {
char x = stk.back();
stk.pop_back();
stk.pop_back();
stk.push_back(x);
} else {
stk.pop_back();
}
cond = false;
} else {
stk.push_back(c);
}
}
}
return {stk[0]};
}
};
```

### **Go**

```go
func parseTernary(expression string) string {
stk := []byte{}
cond := false
for i := len(expression) - 1; i >= 0; i-- {
c := expression[i]
if c == ':' {
continue
}
if c == '?' {
cond = true
} else {
if cond {
if c == 'T' {
x := stk[len(stk)-1]
stk = stk[:len(stk)-2]
stk = append(stk, x)
} else {
stk = stk[:len(stk)-1]
}
cond = false
} else {
stk = append(stk, c)
}
}
}
return string(stk[0])
}
```

### **...**
Expand Down
118 changes: 117 additions & 1 deletion solution/0400-0499/0439.Ternary Expression Parser/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,129 @@ or &quot;(F ? 1 : (T ? 4 : 5))&quot; --&gt; &quot;(T ? 4 : 5)&quot; --&gt; &quot
### **Python3**

```python

class Solution:
def parseTernary(self, expression: str) -> str:
stk = []
cond = False
for c in expression[::-1]:
if c == ':':
continue
if c == '?':
cond = True
else:
if cond:
if c == 'T':
x = stk.pop()
stk.pop()
stk.append(x)
else:
stk.pop()
cond = False
else:
stk.append(c)
return stk[0]
```

### **Java**

```java
class Solution {
public String parseTernary(String expression) {
Deque<Character> stk = new ArrayDeque<>();
boolean cond = false;
for (int i = expression.length() - 1; i >= 0; --i) {
char c = expression.charAt(i);
if (c == ':') {
continue;
}
if (c == '?') {
cond = true;
} else {
if (cond) {
if (c == 'T') {
char x = stk.pop();
stk.pop();
stk.push(x);
} else {
stk.pop();
}
cond = false;
} else {
stk.push(c);
}
}
}
return String.valueOf(stk.peek());
}
}
```

### **C++**

```cpp
class Solution {
public:
string parseTernary(string expression) {
string stk;
bool cond = false;
reverse(expression.begin(), expression.end());
for (char& c : expression) {
if (c == ':') {
continue;
}
if (c == '?') {
cond = true;
} else {
if (cond) {
if (c == 'T') {
char x = stk.back();
stk.pop_back();
stk.pop_back();
stk.push_back(x);
} else {
stk.pop_back();
}
cond = false;
} else {
stk.push_back(c);
}
}
}
return {stk[0]};
}
};
```

### **Go**

```go
func parseTernary(expression string) string {
stk := []byte{}
cond := false
for i := len(expression) - 1; i >= 0; i-- {
c := expression[i]
if c == ':' {
continue
}
if c == '?' {
cond = true
} else {
if cond {
if c == 'T' {
x := stk[len(stk)-1]
stk = stk[:len(stk)-2]
stk = append(stk, x)
} else {
stk = stk[:len(stk)-1]
}
cond = false
} else {
stk = append(stk, c)
}
}
}
return string(stk[0])
}
```

### **...**
Expand Down
31 changes: 31 additions & 0 deletions solution/0400-0499/0439.Ternary Expression Parser/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Solution {
public:
string parseTernary(string expression) {
string stk;
bool cond = false;
reverse(expression.begin(), expression.end());
for (char& c : expression) {
if (c == ':') {
continue;
}
if (c == '?') {
cond = true;
} else {
if (cond) {
if (c == 'T') {
char x = stk.back();
stk.pop_back();
stk.pop_back();
stk.push_back(x);
} else {
stk.pop_back();
}
cond = false;
} else {
stk.push_back(c);
}
}
}
return {stk[0]};
}
};
27 changes: 27 additions & 0 deletions solution/0400-0499/0439.Ternary Expression Parser/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
func parseTernary(expression string) string {
stk := []byte{}
cond := false
for i := len(expression) - 1; i >= 0; i-- {
c := expression[i]
if c == ':' {
continue
}
if c == '?' {
cond = true
} else {
if cond {
if c == 'T' {
x := stk[len(stk)-1]
stk = stk[:len(stk)-2]
stk = append(stk, x)
} else {
stk = stk[:len(stk)-1]
}
cond = false
} else {
stk = append(stk, c)
}
}
}
return string(stk[0])
}
29 changes: 29 additions & 0 deletions solution/0400-0499/0439.Ternary Expression Parser/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Solution {
public String parseTernary(String expression) {
Deque<Character> stk = new ArrayDeque<>();
boolean cond = false;
for (int i = expression.length() - 1; i >= 0; --i) {
char c = expression.charAt(i);
if (c == ':') {
continue;
}
if (c == '?') {
cond = true;
} else {
if (cond) {
if (c == 'T') {
char x = stk.pop();
stk.pop();
stk.push(x);
} else {
stk.pop();
}
cond = false;
} else {
stk.push(c);
}
}
}
return String.valueOf(stk.peek());
}
}
21 changes: 21 additions & 0 deletions solution/0400-0499/0439.Ternary Expression Parser/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution:
def parseTernary(self, expression: str) -> str:
stk = []
cond = False
for c in expression[::-1]:
if c == ':':
continue
if c == '?':
cond = True
else:
if cond:
if c == 'T':
x = stk.pop()
stk.pop()
stk.append(x)
else:
stk.pop()
cond = False
else:
stk.append(c)
return stk[0]
Loading