Skip to content

Commit e326a8e

Browse files
committed
feat: add solutions to lc problem: No.1317
No.1317.Convert Integer to the Sum of Two No-Zero Integers
1 parent bfbb28c commit e326a8e

File tree

10 files changed

+147
-59
lines changed

10 files changed

+147
-59
lines changed

solution/1100-1199/1106.Parsing A Boolean Expression/README.md

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,9 @@ class Solution {
121121
stk.pop();
122122
}
123123
char op = stk.pop();
124-
if (op == '!') {
125-
c = f > 0 ? 't' : 'f';
126-
} else if (op == '&') {
127-
c = f > 0 ? 'f' : 't';
128-
} else {
129-
c = t > 0 ? 't' : 'f';
124+
c = 'f';
125+
if ((op == '!' && f > 0) || (op == '&' && f == 0) || (op == '|' && t > 0)) {
126+
c = 't';
130127
}
131128
stk.push(c);
132129
}
@@ -186,19 +183,8 @@ func parseBoolExpr(expression string) bool {
186183
op := stk[len(stk)-1]
187184
stk = stk[:len(stk)-1]
188185
c = 'f'
189-
switch op {
190-
case '!':
191-
if f > 0 {
192-
c = 't'
193-
}
194-
case '&':
195-
if f == 0 {
196-
c = 't'
197-
}
198-
case '|':
199-
if t > 0 {
200-
c = 't'
201-
}
186+
if (op == '!' && f > 0) || (op == '&' && f == 0) || (op == '|' && t > 0) {
187+
c = 't'
202188
}
203189
stk = append(stk, c)
204190
}

solution/1100-1199/1106.Parsing A Boolean Expression/README_EN.md

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,9 @@ class Solution {
104104
stk.pop();
105105
}
106106
char op = stk.pop();
107-
if (op == '!') {
108-
c = f > 0 ? 't' : 'f';
109-
} else if (op == '&') {
110-
c = f > 0 ? 'f' : 't';
111-
} else {
112-
c = t > 0 ? 't' : 'f';
107+
c = 'f';
108+
if ((op == '!' && f > 0) || (op == '&' && f == 0) || (op == '|' && t > 0)) {
109+
c = 't';
113110
}
114111
stk.push(c);
115112
}
@@ -169,19 +166,8 @@ func parseBoolExpr(expression string) bool {
169166
op := stk[len(stk)-1]
170167
stk = stk[:len(stk)-1]
171168
c = 'f'
172-
switch op {
173-
case '!':
174-
if f > 0 {
175-
c = 't'
176-
}
177-
case '&':
178-
if f == 0 {
179-
c = 't'
180-
}
181-
case '|':
182-
if t > 0 {
183-
c = 't'
184-
}
169+
if (op == '!' && f > 0) || (op == '&' && f == 0) || (op == '|' && t > 0) {
170+
c = 't'
185171
}
186172
stk = append(stk, c)
187173
}

solution/1100-1199/1106.Parsing A Boolean Expression/Solution.go

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,8 @@ func parseBoolExpr(expression string) bool {
1616
op := stk[len(stk)-1]
1717
stk = stk[:len(stk)-1]
1818
c = 'f'
19-
switch op {
20-
case '!':
21-
if f > 0 {
22-
c = 't'
23-
}
24-
case '&':
25-
if f == 0 {
26-
c = 't'
27-
}
28-
case '|':
29-
if t > 0 {
30-
c = 't'
31-
}
19+
if (op == '!' && f > 0) || (op == '&' && f == 0) || (op == '|' && t > 0) {
20+
c = 't'
3221
}
3322
stk = append(stk, c)
3423
}

solution/1100-1199/1106.Parsing A Boolean Expression/Solution.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,9 @@ public boolean parseBoolExpr(String expression) {
1313
stk.pop();
1414
}
1515
char op = stk.pop();
16-
if (op == '!') {
17-
c = f > 0 ? 't' : 'f';
18-
} else if (op == '&') {
19-
c = f > 0 ? 'f' : 't';
20-
} else {
21-
c = t > 0 ? 't' : 'f';
16+
c = 'f';
17+
if ((op == '!' && f > 0) || (op == '&' && f == 0) || (op == '|' && t > 0)) {
18+
c = 't';
2219
}
2320
stk.push(c);
2421
}

solution/1300-1399/1317.Convert Integer to the Sum of Two No-Zero Integers/README.md

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,22 +64,72 @@
6464

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

67+
**方法一:直接枚举**
68+
69+
从 $1$ 开始枚举 $a$,判断 $a$ 和 $n - a$ 是否满足条件,如果满足则返回。
70+
71+
时间复杂度 $O(n\times \log n)$。
72+
6773
<!-- tabs:start -->
6874

6975
### **Python3**
7076

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

7379
```python
74-
80+
class Solution:
81+
def getNoZeroIntegers(self, n: int) -> List[int]:
82+
for a in range(1, n):
83+
b = n - a
84+
if "0" not in str(a) + str(b):
85+
return [a, b]
7586
```
7687

7788
### **Java**
7889

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

8192
```java
93+
class Solution {
94+
public int[] getNoZeroIntegers(int n) {
95+
for (int a = 1; a < n; ++a) {
96+
int b = n - a;
97+
if (!(a + "" + b).contains("0")) {
98+
return new int[] {a, b};
99+
}
100+
}
101+
return null;
102+
}
103+
}
104+
```
105+
106+
### **C++**
107+
108+
```cpp
109+
class Solution {
110+
public:
111+
vector<int> getNoZeroIntegers(int n) {
112+
for (int a = 1; a < n; ++a) {
113+
int b = n - a;
114+
if ((to_string(a) + to_string(b)).find('0') == string::npos) return {a, b};
115+
}
116+
return {};
117+
}
118+
};
119+
```
82120
121+
### **Go**
122+
123+
```go
124+
func getNoZeroIntegers(n int) []int {
125+
for a := 1; a < n; a++ {
126+
b := n - a
127+
if !strings.Contains(strconv.Itoa(a)+strconv.Itoa(b), "0") {
128+
return []int{a, b}
129+
}
130+
}
131+
return nil
132+
}
83133
```
84134

85135
### **...**

solution/1300-1399/1317.Convert Integer to the Sum of Two No-Zero Integers/README_EN.md

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,57 @@
4545
### **Python3**
4646

4747
```python
48-
48+
class Solution:
49+
def getNoZeroIntegers(self, n: int) -> List[int]:
50+
for a in range(1, n):
51+
b = n - a
52+
if "0" not in str(a) + str(b):
53+
return [a, b]
4954
```
5055

5156
### **Java**
5257

5358
```java
59+
class Solution {
60+
public int[] getNoZeroIntegers(int n) {
61+
for (int a = 1; a < n; ++a) {
62+
int b = n - a;
63+
if (!(a + "" + b).contains("0")) {
64+
return new int[] {a, b};
65+
}
66+
}
67+
return null;
68+
}
69+
}
70+
```
71+
72+
### **C++**
73+
74+
```cpp
75+
class Solution {
76+
public:
77+
vector<int> getNoZeroIntegers(int n) {
78+
for (int a = 1; a < n; ++a) {
79+
int b = n - a;
80+
if ((to_string(a) + to_string(b)).find('0') == string::npos) return {a, b};
81+
}
82+
return {};
83+
}
84+
};
85+
```
5486
87+
### **Go**
88+
89+
```go
90+
func getNoZeroIntegers(n int) []int {
91+
for a := 1; a < n; a++ {
92+
b := n - a
93+
if !strings.Contains(strconv.Itoa(a)+strconv.Itoa(b), "0") {
94+
return []int{a, b}
95+
}
96+
}
97+
return nil
98+
}
5599
```
56100

57101
### **...**
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
public:
3+
vector<int> getNoZeroIntegers(int n) {
4+
for (int a = 1; a < n; ++a) {
5+
int b = n - a;
6+
if ((to_string(a) + to_string(b)).find('0') == string::npos) return {a, b};
7+
}
8+
return {};
9+
}
10+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
func getNoZeroIntegers(n int) []int {
2+
for a := 1; a < n; a++ {
3+
b := n - a
4+
if !strings.Contains(strconv.Itoa(a)+strconv.Itoa(b), "0") {
5+
return []int{a, b}
6+
}
7+
}
8+
return nil
9+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public int[] getNoZeroIntegers(int n) {
3+
for (int a = 1; a < n; ++a) {
4+
int b = n - a;
5+
if (!(a + "" + b).contains("0")) {
6+
return new int[] {a, b};
7+
}
8+
}
9+
return null;
10+
}
11+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution:
2+
def getNoZeroIntegers(self, n: int) -> List[int]:
3+
for a in range(1, n):
4+
b = n - a
5+
if "0" not in str(a) + str(b):
6+
return [a, b]

0 commit comments

Comments
 (0)