Skip to content

Commit b905cca

Browse files
committed
fix: display lc problem: No.0020 solutions
No.0020.Valid Parentheses
1 parent 54fe09b commit b905cca

File tree

3 files changed

+223
-1
lines changed

3 files changed

+223
-1
lines changed

solution/0000-0099/0020.Valid Parentheses/README.md

+109
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,115 @@ private:
133133
};
134134
```
135135
136+
### **Go**
137+
138+
```go
139+
func isValid(s string) bool {
140+
stack := newStack()
141+
for _, str := range s {
142+
if str == '(' || str == '[' || str == '{' {
143+
stack.push(byte(str))
144+
} else if str == ')' {
145+
if stack.pop() != (byte('(')) {
146+
return false
147+
}
148+
} else if str == ']' {
149+
if stack.pop() != (byte('[')) {
150+
return false
151+
}
152+
} else if str == '}' {
153+
if stack.pop() != (byte('{')) {
154+
return false
155+
}
156+
}
157+
}
158+
return stack.size() == 0
159+
}
160+
161+
type Stack struct {
162+
data []byte
163+
index int
164+
}
165+
166+
func newStack() *Stack {
167+
return &Stack{
168+
data: make([]byte, 10),
169+
}
170+
}
171+
172+
func (s *Stack) pop() byte {
173+
if s.index == 0 {
174+
return 0
175+
}
176+
s.index--
177+
r := s.data[s.index]
178+
return r
179+
}
180+
181+
func (s *Stack) push(b byte) {
182+
if len(s.data) - 1 <= s.index {
183+
newData := make([]byte, len(s.data))
184+
s.data = append(s.data, newData[:]...)
185+
}
186+
s.data[s.index] = b
187+
s.index++
188+
}
189+
190+
func (s *Stack) size() int {
191+
return s.index
192+
}
193+
```
194+
195+
### **JavaScript**
196+
197+
```js
198+
/**
199+
* @param {string} s
200+
* @return {boolean}
201+
*/
202+
var isValid = function (s) {
203+
let arr = [];
204+
for (let i = 0; i < s.length; i++) {
205+
if (s[i] === '{' || s[i] === '[' || s[i] === '(') {
206+
arr.push(s[i]);
207+
} else {
208+
if (s[i] === ')' && arr[arr.length - 1] === '(') arr.pop();
209+
else if (s[i] === ']' && arr[arr.length - 1] === '[') arr.pop();
210+
else if (s[i] === '}' && arr[arr.length - 1] === '{') arr.pop();
211+
else return false;
212+
}
213+
}
214+
return arr.length === 0;
215+
};
216+
```
217+
218+
### **Ruby**
219+
220+
```rb
221+
# @param {String} s
222+
# @return {Boolean}
223+
def is_valid(s)
224+
stack = ''
225+
s.split('').each do |c|
226+
if ['{', '[', '('].include?(c)
227+
stack += c
228+
else
229+
if c == '}' && stack[stack.length - 1] == '{'
230+
231+
stack = stack.length > 1 ? stack[0..stack.length - 2] : ""
232+
elsif c == ']' && stack[stack.length - 1] == '['
233+
stack = stack.length > 1 ? stack[0..stack.length - 2] : ""
234+
elsif c == ')' && stack[stack.length - 1] == '('
235+
stack = stack.length > 1 ? stack[0..stack.length - 2] : ""
236+
else
237+
return false
238+
end
239+
end
240+
end
241+
stack == ''
242+
end
243+
```
244+
136245
### **...**
137246

138247
```

solution/0000-0099/0020.Valid Parentheses/README_EN.md

+109
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,115 @@ private:
118118
};
119119
```
120120
121+
### **Go**
122+
123+
```go
124+
func isValid(s string) bool {
125+
stack := newStack()
126+
for _, str := range s {
127+
if str == '(' || str == '[' || str == '{' {
128+
stack.push(byte(str))
129+
} else if str == ')' {
130+
if stack.pop() != (byte('(')) {
131+
return false
132+
}
133+
} else if str == ']' {
134+
if stack.pop() != (byte('[')) {
135+
return false
136+
}
137+
} else if str == '}' {
138+
if stack.pop() != (byte('{')) {
139+
return false
140+
}
141+
}
142+
}
143+
return stack.size() == 0
144+
}
145+
146+
type Stack struct {
147+
data []byte
148+
index int
149+
}
150+
151+
func newStack() *Stack {
152+
return &Stack{
153+
data: make([]byte, 10),
154+
}
155+
}
156+
157+
func (s *Stack) pop() byte {
158+
if s.index == 0 {
159+
return 0
160+
}
161+
s.index--
162+
r := s.data[s.index]
163+
return r
164+
}
165+
166+
func (s *Stack) push(b byte) {
167+
if len(s.data) - 1 <= s.index {
168+
newData := make([]byte, len(s.data))
169+
s.data = append(s.data, newData[:]...)
170+
}
171+
s.data[s.index] = b
172+
s.index++
173+
}
174+
175+
func (s *Stack) size() int {
176+
return s.index
177+
}
178+
```
179+
180+
### **JavaScript**
181+
182+
```js
183+
/**
184+
* @param {string} s
185+
* @return {boolean}
186+
*/
187+
var isValid = function (s) {
188+
let arr = [];
189+
for (let i = 0; i < s.length; i++) {
190+
if (s[i] === '{' || s[i] === '[' || s[i] === '(') {
191+
arr.push(s[i]);
192+
} else {
193+
if (s[i] === ')' && arr[arr.length - 1] === '(') arr.pop();
194+
else if (s[i] === ']' && arr[arr.length - 1] === '[') arr.pop();
195+
else if (s[i] === '}' && arr[arr.length - 1] === '{') arr.pop();
196+
else return false;
197+
}
198+
}
199+
return arr.length === 0;
200+
};
201+
```
202+
203+
### **Ruby**
204+
205+
```rb
206+
# @param {String} s
207+
# @return {Boolean}
208+
def is_valid(s)
209+
stack = ''
210+
s.split('').each do |c|
211+
if ['{', '[', '('].include?(c)
212+
stack += c
213+
else
214+
if c == '}' && stack[stack.length - 1] == '{'
215+
216+
stack = stack.length > 1 ? stack[0..stack.length - 2] : ""
217+
elsif c == ']' && stack[stack.length - 1] == '['
218+
stack = stack.length > 1 ? stack[0..stack.length - 2] : ""
219+
elsif c == ')' && stack[stack.length - 1] == '('
220+
stack = stack.length > 1 ? stack[0..stack.length - 2] : ""
221+
else
222+
return false
223+
end
224+
end
225+
end
226+
stack == ''
227+
end
228+
```
229+
121230
### **...**
122231

123232
```

solution/0000-0099/0020.Valid Parentheses/Solution.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
const isValid = function (s) {
1+
/**
2+
* @param {string} s
3+
* @return {boolean}
4+
*/
5+
var isValid = function (s) {
26
let arr = [];
37
for (let i = 0; i < s.length; i++) {
48
if (s[i] === '{' || s[i] === '[' || s[i] === '(') {

0 commit comments

Comments
 (0)