[英]Valid Parentheses Java
I am trying to solve the Valid Parentheses problem in Java (described here among other places: Valid Parentheses )我正在尝试解决 Java 中的有效括号问题(此处在其他地方进行了描述:有效括号)
My code is as follows thus far:到目前为止,我的代码如下:
class Solution {
public boolean isValid(String s) {
if(s.charAt(0) ="(") {
if(s.charAt(s.length-1) != ")"){
system.out.println("false");
}
}
if(s.charAt(0) ="[") {
if(s.charAt(s.length-1) != "]"){
system.out.println("false");
}
}
if(s.charAt(0) ="{") {
if(s.charAt(s.length-1) != "}"){
system.out.println("false");
}
}
}
}
Currently I am getting the following error:目前我收到以下错误:
Line 4: error: unexpected type
第 4 行:错误:
unexpected type
if(s.charAt(0) ="("){
Can anyone advise on what the issue is?任何人都可以就问题所在提出建议吗? I can't at the moment figure it out but feel like it's something simple I'm overlooking.
我现在无法弄清楚,但觉得这是我忽略的简单事情。
Two immediate issues:两个紧迫的问题:
char c = 'c';
char c = 'c';
==
.==
完成的。 The single equals sign =
is used for assignment.=
用于赋值。 So that line should be:所以那一行应该是:
if(s.charAt(0) == '(') { /* ... */ }
Note that this only fixes the most immediate error you're experiencing.请注意,这只会修复您遇到的最直接的错误。 After fixing that, there are some more, for example
system.out.println()
should be System.out.println()
.修复之后,还有一些,例如
system.out.println()
应该是System.out.println()
。
And, after fixing that, I think you're still a way off from solving the actual assignment.而且,在解决这个问题之后,我认为你离解决实际任务还有一段距离。 Just keep at it & good luck!
坚持下去,祝你好运!
There are some issues in your answer.你的回答有些问题。 Robby Cornelissen points you out some issues in his answer .
Robby Cornelissen在他的回答中指出了一些问题。 And I also saw 2 errors in your code.
而且我还在您的代码中看到了 2 个错误。
s.length
should be s.length()
. s.length
应该是s.length()
。isValid
method.isValid
方法中没有返回语句。 Issue 1第 1 期
In the following statement, you are missing the brackets ()
after the length.在以下语句中,长度后缺少方括号
()
。 Length is a method in Java that is used to get the length of a Java String that you know already. Length 是 Java 中的一种方法,用于获取您已知的 Java String 的长度。
if(s.charAt(s.length - 1) != ')')
So your above statement should be as follows.所以你上面的陈述应该如下。
if(s.charAt(s.length() - 1) != ')')
Issue 2第 2 期
In your isValid
method it's returning a boolean
value.在您的
isValid
方法中,它返回一个boolean
值。 The return type of that method is boolean
.该方法的返回类型是
boolean
。 But you are not returning any boolean value inside your method.但是您没有在方法中返回任何布尔值。 So you have to add a return statement.
所以你必须添加一个return语句。 It may be,
可能是,
return true;
or return false;
或
return false;
or any boolean variable
declared inside isValid
method.或在
isValid
方法中声明的任何boolean variable
。
it is best to use stack for valid parentheses in Java最好在 Java 中使用堆栈作为有效括号
class Solution {
public boolean isValid(String s) {
Stack stack = new Stack();
for(int i=0; i<s.length; i++){
if(s[i] =='('){
stack.push(')')
}
else if(s[i] == '['){
stack.push(']')
}
else if(s[i] =='{'){
stack.push('}')
}
else if(s[i] !== stack.pop()){
return false;
}
}
return true;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.