Skip to content

Commit 98329ef

Browse files
committed
feat: add solutions to lc problems: No.2011,2012
1 parent eb400b3 commit 98329ef

File tree

12 files changed

+406
-8
lines changed

12 files changed

+406
-8
lines changed

solution/2000-2099/2011.Final Value of Variable After Performing Operations/README.md

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,25 @@ X--:X 减 1 ,X = 1 - 1 = 0
7777
<!-- 这里可写当前语言的特殊实现逻辑 -->
7878

7979
```python
80-
80+
class Solution:
81+
def finalValueAfterOperations(self, operations: List[str]) -> int:
82+
return sum(1 if s[1] == '+' else -1 for s in operations)
8183
```
8284

8385
### **Java**
8486

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

8789
```java
88-
90+
class Solution {
91+
public int finalValueAfterOperations(String[] operations) {
92+
int ans = 0;
93+
for (String s : operations) {
94+
ans += (s.charAt(1) == '+' ? 1 : -1);
95+
}
96+
return ans;
97+
}
98+
}
8999
```
90100

91101
### **TypeScript**
@@ -100,6 +110,35 @@ function finalValueAfterOperations(operations: string[]): number {
100110
};
101111
```
102112

113+
### **C++**
114+
115+
```cpp
116+
class Solution {
117+
public:
118+
int finalValueAfterOperations(vector<string>& operations) {
119+
int ans = 0;
120+
for (auto s : operations) ans += (s[1] == '+' ? 1 : -1);
121+
return ans;
122+
}
123+
};
124+
```
125+
126+
### **Go**
127+
128+
```go
129+
func finalValueAfterOperations(operations []string) int {
130+
ans := 0
131+
for _, s := range operations {
132+
if s[1] == '+' {
133+
ans += 1
134+
} else {
135+
ans -= 1
136+
}
137+
}
138+
return ans
139+
}
140+
```
141+
103142
### **...**
104143

105144
```

solution/2000-2099/2011.Final Value of Variable After Performing Operations/README_EN.md

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,23 @@ X--: X is decremented by 1, X = 1 - 1 = 0.
6969
### **Python3**
7070

7171
```python
72-
72+
class Solution:
73+
def finalValueAfterOperations(self, operations: List[str]) -> int:
74+
return sum(1 if s[1] == '+' else -1 for s in operations)
7375
```
7476

7577
### **Java**
7678

7779
```java
78-
80+
class Solution {
81+
public int finalValueAfterOperations(String[] operations) {
82+
int ans = 0;
83+
for (String s : operations) {
84+
ans += (s.charAt(1) == '+' ? 1 : -1);
85+
}
86+
return ans;
87+
}
88+
}
7989
```
8090

8191
### **TypeScript**
@@ -90,6 +100,35 @@ function finalValueAfterOperations(operations: string[]): number {
90100
};
91101
```
92102

103+
### **C++**
104+
105+
```cpp
106+
class Solution {
107+
public:
108+
int finalValueAfterOperations(vector<string>& operations) {
109+
int ans = 0;
110+
for (auto s : operations) ans += (s[1] == '+' ? 1 : -1);
111+
return ans;
112+
}
113+
};
114+
```
115+
116+
### **Go**
117+
118+
```go
119+
func finalValueAfterOperations(operations []string) int {
120+
ans := 0
121+
for _, s := range operations {
122+
if s[1] == '+' {
123+
ans += 1
124+
} else {
125+
ans -= 1
126+
}
127+
}
128+
return ans
129+
}
130+
```
131+
93132
### **...**
94133

95134
```
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution {
2+
public:
3+
int finalValueAfterOperations(vector<string>& operations) {
4+
int ans = 0;
5+
for (auto s : operations) ans += (s[1] == '+' ? 1 : -1);
6+
return ans;
7+
}
8+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
func finalValueAfterOperations(operations []string) int {
2+
ans := 0
3+
for _, s := range operations {
4+
if s[1] == '+' {
5+
ans += 1
6+
} else {
7+
ans -= 1
8+
}
9+
}
10+
return ans
11+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution {
2+
public int finalValueAfterOperations(String[] operations) {
3+
int ans = 0;
4+
for (String s : operations) {
5+
ans += (s.charAt(1) == '+' ? 1 : -1);
6+
}
7+
return ans;
8+
}
9+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def finalValueAfterOperations(self, operations: List[str]) -> int:
3+
return sum(1 if s[1] == '+' else -1 for s in operations)

solution/2000-2099/2012.Sum of Beauty in the Array/README.md

Lines changed: 101 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,52 @@
6363
<!-- 这里可写当前语言的特殊实现逻辑 -->
6464

6565
```python
66-
66+
class Solution:
67+
def sumOfBeauties(self, nums: List[int]) -> int:
68+
n = len(nums)
69+
lmx = [0] * n
70+
for i in range(1, n):
71+
lmx[i] = max(lmx[i - 1], nums[i - 1])
72+
rmi = [100001] * n
73+
for i in range(n - 2, -1, -1):
74+
rmi[i] = min(rmi[i + 1], nums[i + 1])
75+
ans = 0
76+
for i in range(1, n - 1):
77+
if lmx[i] < nums[i] < rmi[i]:
78+
ans += 2
79+
elif nums[i - 1] < nums[i] < nums[i + 1]:
80+
ans += 1
81+
return ans
6782
```
6883

6984
### **Java**
7085

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

7388
```java
74-
89+
class Solution {
90+
public int sumOfBeauties(int[] nums) {
91+
int n = nums.length;
92+
int[] lmx = new int[n];
93+
int[] rmi = new int[n];
94+
rmi[n - 1] = 100001;
95+
for (int i = 1; i < n; ++i) {
96+
lmx[i] = Math.max(lmx[i - 1], nums[i - 1]);
97+
}
98+
for (int i = n - 2; i >= 0; --i) {
99+
rmi[i] = Math.min(rmi[i + 1], nums[i + 1]);
100+
}
101+
int ans = 0;
102+
for (int i = 1; i < n - 1; ++i) {
103+
if (lmx[i] < nums[i] && nums[i] < rmi[i]) {
104+
ans += 2;
105+
} else if (nums[i - 1] < nums[i] && nums[i] < nums[i + 1]) {
106+
ans += 1;
107+
}
108+
}
109+
return ans;
110+
}
111+
}
75112
```
76113

77114
### **TypeScript
@@ -98,6 +135,68 @@ function sumOfBeauties(nums: number[]): number {
98135
};
99136
```
100137

138+
### **C++**
139+
140+
```cpp
141+
class Solution {
142+
public:
143+
int sumOfBeauties(vector<int>& nums) {
144+
int n = nums.size();
145+
vector<int> lmx(n);
146+
vector<int> rmi(n, 100001);
147+
for (int i = 1; i < n; ++i) lmx[i] = max(lmx[i - 1], nums[i - 1]);
148+
for (int i = n - 2; i >= 0; --i) rmi[i] = min(rmi[i + 1], nums[i + 1]);
149+
int ans = 0;
150+
for (int i = 1; i < n - 1; ++i)
151+
{
152+
if (lmx[i] < nums[i] && nums[i] < rmi[i]) ans += 2;
153+
else if (nums[i - 1] < nums[i] && nums[i] < nums[i + 1]) ans += 1;
154+
}
155+
return ans;
156+
}
157+
};
158+
```
159+
160+
### **Go**
161+
162+
```go
163+
func sumOfBeauties(nums []int) int {
164+
n := len(nums)
165+
lmx := make([]int, n)
166+
rmi := make([]int, n)
167+
rmi[n-1] = 100001
168+
for i := 1; i < n; i++ {
169+
lmx[i] = max(lmx[i-1], nums[i-1])
170+
}
171+
for i := n - 2; i >= 0; i-- {
172+
rmi[i] = min(rmi[i+1], nums[i+1])
173+
}
174+
ans := 0
175+
for i := 1; i < n-1; i++ {
176+
if lmx[i] < nums[i] && nums[i] < rmi[i] {
177+
ans += 2
178+
} else if nums[i-1] < nums[i] && nums[i] < nums[i+1] {
179+
ans += 1
180+
}
181+
}
182+
return ans
183+
}
184+
185+
func max(a, b int) int {
186+
if a > b {
187+
return a
188+
}
189+
return b
190+
}
191+
192+
func min(a, b int) int {
193+
if a < b {
194+
return a
195+
}
196+
return b
197+
}
198+
```
199+
101200
### **...**
102201

103202
```

0 commit comments

Comments
 (0)