These are my codes:
public class Solution {
public boolean isBalanced(String s) {
Stack<Character> stack = new Stack<Character>();
char c;
for(int i = 0; i<s.length(); i++) {
c = s.charAt(i);
if(c == '[' || c == '(') {
stack.push(c);
}
else if(c == ']' || c == ')'){
if(stack.isEmpty()) {
return false;
}
else if(c == ']' && stack.peek() == '[') {
stack.pop();}
else if(c == ')' && stack.peek() == '(') {
stack.pop();
}
else {
return false;
}
if (stack.isEmpty()){
return true;
}
}
}
return false;
}
public static void main(String[] args) {
Solution solution = new Solution();
System.out.println(solution.isBalanced("()")); // should be true
System.out.println(solution.isBalanced("(")); // should be false
System.out.println(solution.isBalanced("(])")); // should be false
}
}
and I am getting this error:
Solution.java:4: error: duplicate class: a22629.Solution
public class Solution {
^
MyTests.java:6: error: cannot access Solution
Solution solution;
^
bad source file: ./Solution.java
file does not contain class Solution
Please remove or make sure it appears in the correct subdirectory of the sourcepath.
2 errors
Not compiled
This is the full error I am getting and I am not sure why.
When I compile it in terminal it works, but when I want to submit it to my class portal, it does not work and gives me this error.
Please share more code Or add the command line data of your successful compilation and invocation.
default
access scope for your class. Ensure that your class is public
to be accessible to other's outside of your package. (Though locally you can still compile and run the class independently, other classes outside of your package can not access your class)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.