挺好写的,我感觉。
public static boolean func(char[] arr) {
int length = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == 'O') length--;
if (arr[i] == 'I') length++;
if (length < 0) return false;
}
return length == 0;
}
答案还没我效率高。。。
4、判断回文链表、在力扣上刷到过
public static boolean isPalindrome(linkedList head) {
linkedList p = head.next;
linkedList q = new linkedList();
while (p != null) {
q.next = new linkedList(p.val, q.next);
p = p.next;
}
p = head.next;
q = q.next;
while (q != null) {
if (q.val != p.val) {
return false;
}
p = p.next;
q = q.next;
}
return true;
}
坑逼,这个题没说传参可以传链表长度。。。传链表长度的话也好整。不用链栈,用数组栈。
5、共享栈还从来没试过呢
class SharedStack {
private final int[] stack;
public Stack1 s1;
public Stack2 s2;
public SharedStack(int maxSize) {
stack = new int[maxSize];
s1 = new Stack1();
s2 = new Stack2();
}
public class Stack1 {
private int top = -1;
public void push(int value) {
if (top == s2.top - 1) {
throw new IndexOutOfBoundsException();
}
stack[++top] = value;
}
public int pop() {
if (top < 0) {
throw new EmptyStackException();
}
return stack[top--];
}
private Stack1() {
}
}
public class Stack2 {
private int top = stack.length;
public void push(int value) {
if (s1.top == top - 1) {
throw new IndexOutOfBoundsException();
}
stack[--top] = value;
}
public int pop() {
if (top > stack.length - 1) {
throw new EmptyStackException();
}
return stack[top++];
}
private Stack2 () {
}
}
}
答案是面向模块设计的,和我这个差别有点大。



