Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a9da5ca

Browse files
committed
feat: add solutions to lc problems: No.1426,1614
* No.1426.Counting Elements * No.1614.Maximum Nesting Depth of the Parentheses
1 parent c6056b6 commit a9da5ca

File tree

12 files changed

+324
-7
lines changed

12 files changed

+324
-7
lines changed

‎solution/1400-1499/1426.Counting Elements/README.md‎

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
<li><code>0 &lt;= arr[i] &lt;= 1000</code></li>
4949
</ul>
5050

51-
5251
## 解法
5352

5453
<!-- 这里可写通用的实现逻辑 -->
@@ -60,15 +59,70 @@
6059
<!-- 这里可写当前语言的特殊实现逻辑 -->
6160

6261
```python
63-
62+
class Solution:
63+
def countElements(self, arr: List[int]) -> int:
64+
s = set(arr)
65+
res = 0
66+
for num in arr:
67+
if num + 1 in s:
68+
res += 1
69+
return res
6470
```
6571

6672
### **Java**
6773

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

7076
```java
77+
class Solution {
78+
public int countElements(int[] arr) {
79+
Set<Integer> s = new HashSet<>();
80+
for (int num : arr) {
81+
s.add(num);
82+
}
83+
int res = 0;
84+
for (int num : arr) {
85+
if (s.contains(num + 1)) {
86+
++res;
87+
}
88+
}
89+
return res;
90+
}
91+
}
92+
```
93+
94+
### **C++**
95+
96+
```cpp
97+
class Solution {
98+
public:
99+
int countElements(vector<int>& arr) {
100+
unordered_set<int> s;
101+
for (int num : arr) s.insert(num);
102+
int res = 0;
103+
for (int num : arr)
104+
if (s.count(num + 1)) ++res;
105+
return res;
106+
}
107+
};
108+
```
71109
110+
### **Go**
111+
112+
```go
113+
func countElements(arr []int) int {
114+
s := make(map[int]bool)
115+
for _, num := range arr {
116+
s[num] = true
117+
}
118+
res := 0
119+
for _, num := range arr {
120+
if s[num+1] {
121+
res++
122+
}
123+
}
124+
return res
125+
}
72126
```
73127

74128
### **...**

‎solution/1400-1499/1426.Counting Elements/README_EN.md‎

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,75 @@
5454
<li><code>0 &lt;= arr[i] &lt;= 1000</code></li>
5555
</ul>
5656

57-
5857
## Solutions
5958

6059
<!-- tabs:start -->
6160

6261
### **Python3**
6362

6463
```python
65-
64+
class Solution:
65+
def countElements(self, arr: List[int]) -> int:
66+
s = set(arr)
67+
res = 0
68+
for num in arr:
69+
if num + 1 in s:
70+
res += 1
71+
return res
6672
```
6773

6874
### **Java**
6975

7076
```java
77+
class Solution {
78+
public int countElements(int[] arr) {
79+
Set<Integer> s = new HashSet<>();
80+
for (int num : arr) {
81+
s.add(num);
82+
}
83+
int res = 0;
84+
for (int num : arr) {
85+
if (s.contains(num + 1)) {
86+
++res;
87+
}
88+
}
89+
return res;
90+
}
91+
}
92+
```
93+
94+
### **C++**
95+
96+
```cpp
97+
class Solution {
98+
public:
99+
int countElements(vector<int>& arr) {
100+
unordered_set<int> s;
101+
for (int num : arr) s.insert(num);
102+
int res = 0;
103+
for (int num : arr)
104+
if (s.count(num + 1)) ++res;
105+
return res;
106+
}
107+
};
108+
```
71109
110+
### **Go**
111+
112+
```go
113+
func countElements(arr []int) int {
114+
s := make(map[int]bool)
115+
for _, num := range arr {
116+
s[num] = true
117+
}
118+
res := 0
119+
for _, num := range arr {
120+
if s[num+1] {
121+
res++
122+
}
123+
}
124+
return res
125+
}
72126
```
73127

74128
### **...**
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public:
3+
int countElements(vector<int>& arr) {
4+
unordered_set<int> s;
5+
for (int num : arr) s.insert(num);
6+
int res = 0;
7+
for (int num : arr)
8+
if (s.count(num + 1)) ++res;
9+
return res;
10+
}
11+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
func countElements(arr []int) int {
2+
s := make(map[int]bool)
3+
for _, num := range arr {
4+
s[num] = true
5+
}
6+
res := 0
7+
for _, num := range arr {
8+
if s[num+1] {
9+
res++
10+
}
11+
}
12+
return res
13+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int countElements(int[] arr) {
3+
Set<Integer> s = new HashSet<>();
4+
for (int num : arr) {
5+
s.add(num);
6+
}
7+
int res = 0;
8+
for (int num : arr) {
9+
if (s.contains(num + 1)) {
10+
++res;
11+
}
12+
}
13+
return res;
14+
}
15+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def countElements(self, arr: List[int]) -> int:
3+
s = set(arr)
4+
res = 0
5+
for num in arr:
6+
if num + 1 in s:
7+
res += 1
8+
return res

‎solution/1600-1699/1614.Maximum Nesting Depth of the Parentheses/README.md‎

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@
6868
<li>题目数据保证括号表达式 <code>s</code> 是 <strong>有效的括号表达式</strong></li>
6969
</ul>
7070

71-
7271
## 解法
7372

7473
<!-- 这里可写通用的实现逻辑 -->
@@ -80,15 +79,72 @@
8079
<!-- 这里可写当前语言的特殊实现逻辑 -->
8180

8281
```python
83-
82+
class Solution:
83+
def maxDepth(self, s: str) -> int:
84+
res = depth = 0
85+
for c in s:
86+
if c == '(':
87+
depth += 1
88+
res = max(res, depth)
89+
elif c == ')':
90+
depth -= 1
91+
return res
8492
```
8593

8694
### **Java**
8795

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

9098
```java
99+
class Solution {
100+
public int maxDepth(String s) {
101+
int res = 0, depth = 0;
102+
for (char c : s.toCharArray()) {
103+
if (c == '(') {
104+
res = Math.max(res, ++depth);
105+
} else if (c == ')') {
106+
--depth;
107+
}
108+
}
109+
return res;
110+
}
111+
}
112+
```
113+
114+
### **C++**
115+
116+
```cpp
117+
class Solution {
118+
public:
119+
int maxDepth(string s) {
120+
int res = 0, depth =0;
121+
for (char c : s)
122+
{
123+
if (c == '(') res = max(res, ++depth);
124+
else if (c == ')') --depth;
125+
}
126+
return res;
127+
}
128+
};
129+
```
91130
131+
### **Go**
132+
133+
```go
134+
func maxDepth(s string) int {
135+
res, depth := 0, 0
136+
for _, c := range s {
137+
if c == '(' {
138+
depth++
139+
if depth > res {
140+
res = depth
141+
}
142+
} else if c == ')' {
143+
depth--
144+
}
145+
}
146+
return res
147+
}
92148
```
93149

94150
### **...**

‎solution/1600-1699/1614.Maximum Nesting Depth of the Parentheses/README_EN.md‎

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,70 @@
7272
### **Python3**
7373

7474
```python
75-
75+
class Solution:
76+
def maxDepth(self, s: str) -> int:
77+
res = depth = 0
78+
for c in s:
79+
if c == '(':
80+
depth += 1
81+
res = max(res, depth)
82+
elif c == ')':
83+
depth -= 1
84+
return res
7685
```
7786

7887
### **Java**
7988

8089
```java
90+
class Solution {
91+
public int maxDepth(String s) {
92+
int res = 0, depth = 0;
93+
for (char c : s.toCharArray()) {
94+
if (c == '(') {
95+
res = Math.max(res, ++depth);
96+
} else if (c == ')') {
97+
--depth;
98+
}
99+
}
100+
return res;
101+
}
102+
}
103+
```
104+
105+
### **C++**
106+
107+
```cpp
108+
class Solution {
109+
public:
110+
int maxDepth(string s) {
111+
int res = 0, depth =0;
112+
for (char c : s)
113+
{
114+
if (c == '(') res = max(res, ++depth);
115+
else if (c == ')') --depth;
116+
}
117+
return res;
118+
}
119+
};
120+
```
81121
122+
### **Go**
123+
124+
```go
125+
func maxDepth(s string) int {
126+
res, depth := 0, 0
127+
for _, c := range s {
128+
if c == '(' {
129+
depth++
130+
if depth > res {
131+
res = depth
132+
}
133+
} else if c == ')' {
134+
depth--
135+
}
136+
}
137+
return res
138+
}
82139
```
83140

84141
### **...**
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public:
3+
int maxDepth(string s) {
4+
int res = 0, depth =0;
5+
for (char c : s)
6+
{
7+
if (c == '(') res = max(res, ++depth);
8+
else if (c == ')') --depth;
9+
}
10+
return res;
11+
}
12+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
func maxDepth(s string) int {
2+
res, depth := 0, 0
3+
for _, c := range s {
4+
if c == '(' {
5+
depth++
6+
if depth > res {
7+
res = depth
8+
}
9+
} else if c == ')' {
10+
depth--
11+
}
12+
}
13+
return res
14+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /