目录
1、459.重复的子字符串
巧用kmp的next数组
class Solution {
public boolean repeatedSubstringPattern(String s) {
int[] next = new int[s.length()];
int j = 0;
next[0] = 0;
for (int i = 1; i < s.length(); ++i)
{
while(j > 0 && s.charAt(j) != s.charAt(i))
{
j = next[j - 1];
}
if(s.charAt(j) == s.charAt(i))
{
++j;
}
next[i] = j;
}
if (next[next.length - 1] != 0 && next.length % (next.length - next[next.length - 1]) == 0)
return true;
else
return false;
}
}
2、232.用栈实现队列
需要两个栈
class MyQueue {
Stack<Integer> inStack;
Stack<Integer> outStack;
public MyQueue() {
inStack = new Stack<>();
outStack = new Stack<>();
}
public void push(int x) {
inStack.push(x);
}
public int pop() {
in2out();
return outStack.pop();
}
public int peek() {
in2out();
return outStack.peek();
}
public boolean empty() {
return inStack.isEmpty() && outStack.isEmpty();
}
public void in2out()
{
if(!outStack.isEmpty())
return;
while(!inStack.isEmpty())
outStack.push(inStack.pop());
}
}
3、225. 用队列实现栈
可以用一个队列实现。
注意:LinkedList实现了Queue接口。
Queue接口定义的方法:
- add(element):将指定的元素插入到队尾,如果成功则返回 true,如果队列已满则抛出异常。
- offer(element):将指定的元素插入到队尾,如果成功则返回 true,如果队列已满则返回 false。
- remove():移除并返回队首的元素,如果队列为空则抛出异常。
- poll():移除并返回队首的元素,如果队列为空则返回 null。
- element():返回队首的元素,但不移除它,如果队列为空则抛出异常。
- peek():返回队首的元素,但不移除它,如果队列为空则返回 null。
class MyStack {
Queue<Integer> que;
public MyStack() {
que = new LinkedList<>();
}
public void push(int x) {
que.add(x);
}
public int pop() {
move();
return que.poll();
}
public int top() {
move();
int res = que.peek();
que.add(que.poll());
return res;
}
public boolean empty() {
return que.size() == 0 ? true : false;
}
public void move()
{
int size = que.size();
for(int i = 0; i < size - 1; ++i)
{
que.add(que.poll());
}
}
}
4、20. 有效的括号
三种情况:(1)左括号多了-->最后栈不为空(2)右括号多了-->栈空了但还没有匹配完(3)没配上
class Solution {
public boolean isValid(String s) {
Stack<Character> stack = new Stack<>();
for (int i = 0; i < s.length();++i){
char ch = s.charAt(i);
switch(ch)
{
case '(':
stack.push(')');
break;
case '{':
stack.push('}');
break;
case '[':
stack.push(']');
break;
default:
if(stack.isEmpty() || ch != stack.pop())
return false;
}
}
if(stack.isEmpty())
return true;
else
return false;
}
}
5、1047. 删除字符串中的所有相邻重复项
这不就是祖玛么~
StringBuffer用法:
1、增:public StringBuffer append(String str):可以把任意类型数据添加到字符串缓冲区里面,并返回字符串缓冲区本身
public StringBuffer insert(int offset,String str):在指定位置把任意类型的数据插入到字符串缓冲区里面,并返回字符串缓冲区本身2、删:
public StringBuffer deleteCharAt(int index):删除指定位置的字符,并返回字符串缓冲区本身
public StringBuffer delete(int start,int end):删除从指定位置开始指定位置结束的内容,并返回字符串缓冲区本身3、替换:
public StringBuffer replace(int start,int end,String str):从start开始到end用str替换 也是返回字符串缓冲区本身4、反转:
public StringBuffer reverse():字符串反转 也是返回字符串缓冲区本身5、截取:
public String substring(int start):从指定位置截取到末尾
public String substring(int start,int end):截取从指定位置开始到结束位置,包括开始位置,不包括结束位置
class Solution {
public String removeDuplicates(String s) {
StringBuffer res = new StringBuffer();
int size = -1;
for(int i = 0; i < s.length(); ++i)
{
char ch = s.charAt(i);
if(size != -1 && res.charAt(size) == ch)
{
res.deleteCharAt(size);
--size;
}
else
{
res.append(ch);
++size;
}
}
return res.toString();
}
}
6、150. 逆波兰表达式求值
遇到数字则入栈;遇到运算符则取出栈顶两个数字进行计算,并将结果压入栈中。
class Solution {
public int evalRPN(String[] tokens) {
Deque<Integer> stack = new LinkedList<>();
for(int i = 0; i < tokens.length; ++i)
{
switch(tokens[i])
{
case "+":
stack.push(stack.pop() + stack.pop());
break;
case "-":
stack.push(-stack.pop() + stack.pop());
break;
case "*":
stack.push(stack.pop() * stack.pop());
break;
case "/":
int temp1 = stack.pop();
int temp2 = stack.pop();
stack.push(temp2 / temp1);
break;
default:
stack.push(Integer.valueOf(tokens[i]));
break;
}
}
return stack.pop();
}
}