leecode更新

This commit is contained in:
markilue 2023-01-02 13:28:55 +08:00
parent 04c495fbb9
commit 38fe47b849
10 changed files with 389 additions and 26 deletions

View File

@ -1,7 +1,6 @@
package com.markilue.leecode.stackAndDeque;
import org.junit.Test;
import org.omg.CORBA.PUBLIC_MEMBER;
import java.util.Stack;
@ -27,12 +26,12 @@ import java.util.Stack;
* @Version: 1.0
*/
public class MyQueue {
public class T01_MyQueue {
public Stack<Integer> stack1;
public Stack<Integer> stack2;
public MyQueue() {
public T01_MyQueue() {
stack1=new Stack<>();
stack2=new Stack<>();
@ -88,7 +87,7 @@ public class MyQueue {
@Test
public void test(){
MyQueue myQueue = new MyQueue();
T01_MyQueue myQueue = new T01_MyQueue();
myQueue.push(1); // queue is: [1]
myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
System.out.println(myQueue.peek());// return 1

View File

@ -26,12 +26,12 @@ import java.util.Stack;
* @Version: 1.0
*/
public class MyQueue1 {
public class T01_MyQueue1 {
public Stack<Integer> stack1;
public Stack<Integer> stack2;
public MyQueue1() {
public T01_MyQueue1() {
stack1=new Stack<>();
stack2=new Stack<>();
@ -76,7 +76,7 @@ public class MyQueue1 {
@Test
public void test(){
MyQueue1 myQueue = new MyQueue1();
T01_MyQueue1 myQueue = new T01_MyQueue1();
myQueue.push(1); // queue is: [1]
myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
System.out.println(myQueue.peek());// return 1

View File

@ -4,14 +4,14 @@ import org.junit.Test;
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.concurrent.PriorityBlockingQueue;
/**
* @BelongsProject: Leecode
* @BelongsPackage: com.markilue.leecode.stackAndDeque
* @Author: dingjiawen
* @CreateTime: 2022-09-13 09:59
* @Description: TODO 力扣225题 用队列实现栈:
* @Description:
* TODO 力扣225题 用队列实现栈:
* 请你仅使用两个队列实现一个后入先出LIFO的栈并支持普通栈的全部四种操作pushtoppop empty
* 实现 MyStack
* void push(int x) 将元素 x 压入栈顶
@ -19,19 +19,18 @@ import java.util.concurrent.PriorityBlockingQueue;
* int top() 返回栈顶元素
* boolean empty() 如果栈是空的返回 true 否则返回 false
* <p>
* <p>
* 注意
* 你只能使用队列的基本操作 也就是push to backpeek/pop from frontsize 和is empty这些操作
* 你所使用的语言也许不支持队列你可以使用 list 列表或者 deque双端队列来模拟一个队列, 只要是标准的队列操作即可
* @Version: 1.0
*/
public class MyStack {
public class T02_MyStack {
public Queue<Integer> queue1;
public Queue<Integer> queue2;
public MyStack() {
public T02_MyStack() {
queue1 = new ArrayDeque<>();
queue2 = new ArrayDeque<>();
}
@ -96,7 +95,7 @@ public class MyStack {
@Test
public void test() {
MyStack obj = new MyStack();
T02_MyStack obj = new T02_MyStack();
obj.push(1);
obj.push(2);
obj.push(3);

View File

@ -24,12 +24,12 @@ import java.util.Queue;
* 你所使用的语言也许不支持队列你可以使用 list 列表或者 deque双端队列来模拟一个队列, 只要是标准的队列操作即可
* @Version: 1.0
*/
public class MyStack1 {
public class T02_MyStack1 {
public Queue<Integer> queue1;
public MyStack1() {
public T02_MyStack1() {
queue1 = new ArrayDeque<>();
}
@ -71,7 +71,7 @@ public class MyStack1 {
@Test
public void test() {
MyStack1 obj = new MyStack1();
T02_MyStack1 obj = new T02_MyStack1();
obj.push(1);
obj.push(2);
obj.push(3);

View File

@ -18,7 +18,7 @@ import java.util.Stack;
* 每个右括号都有一个对应的相同类型的左括号
* @Version: 1.0
*/
public class IsValid {
public class T03_IsValid {
@Test
public void test() {
String s = "{([([])])}";

View File

@ -0,0 +1,68 @@
package com.markilue.leecode.stackAndDeque.second;
import java.util.Stack;
/**
*@BelongsProject: Leecode
*@BelongsPackage: com.markilue.leecode.stackAndDeque.second
*@Author: dingjiawen
*@CreateTime: 2023-01-02 11:46
*@Description:
* TODO 二刷leecode232题 用栈实现队列
* 实现 MyQueue
* void push(int x) 将元素 x 推到队列的末尾
* int pop() 从队列的开头移除并返回元素
* int peek() 返回队列开头的元素
* boolean empty() 如果队列为空返回 true 否则返回 false
* 说明
* 你只能使用标准的栈操作 也就是只有push to top,peek/pop from top,size, 和is empty操作是合法的
* 你所使用的语言也许不支持栈你可以使用 list 或者 deque双端队列来模拟一个栈只要是标准的栈操作即可
*@Version: 1.0
*/
public class T01_MyQueue {
//cur维持的是一个队列;temp维持的是一个栈
//入栈时走temp出栈的时候走cur
//当cur没了之后,将temp中的值灌入cur
Stack<Integer> cur;
Stack<Integer> temp;
public T01_MyQueue() {
cur=new Stack<>();
temp=new Stack<>();
}
public void push(int x) {
temp.push(x);
}
public int pop() {
if(!cur.empty()){
//cur有就出cur
return cur.pop();
}else {
//cur没有,就将temp灌入
while (!temp.empty()){
cur.push(temp.pop());
}
return cur.pop();
}
}
public int peek() {
if(!cur.empty()){
return cur.peek();
}else {
//cur没有,就将temp灌入
while (!temp.empty()){
cur.push(temp.pop());
}
return cur.peek();
}
}
public boolean empty() {
return temp.empty()&& cur.empty();
}
}

View File

@ -0,0 +1,70 @@
package com.markilue.leecode.stackAndDeque.second;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
/**
*@BelongsProject: Leecode
*@BelongsPackage: com.markilue.leecode.stackAndDeque.second
*@Author: dingjiawen
*@CreateTime: 2023-01-02 12:23
*@Description:
* TODO 二刷力扣225题 用队列实现栈:
* 请你仅使用两个队列实现一个后入先出LIFO的栈并支持普通栈的全部四种操作pushtoppop empty
* 实现 MyStack
* void push(int x) 将元素 x 压入栈顶
* int pop() 移除并返回栈顶元素
* int top() 返回栈顶元素
* boolean empty() 如果栈是空的返回 true 否则返回 false
* 注意
* 你只能使用队列的基本操作 也就是push to backpeek/pop from frontsize 和is empty这些操作
* 你所使用的语言也许不支持队列你可以使用 list 列表或者 deque双端队列来模拟一个队列, 只要是标准的队列操作即可
*@Version: 1.0
*/
public class T02_MyStack {
//temp当队列,cur当栈
Queue<Integer> temp;
Queue<Integer> cur;
public T02_MyStack() {
temp=new LinkedList<>();
cur=new LinkedList<>();
}
public void push(int x) {
if(temp.isEmpty()){
temp.add(x);
while (!cur.isEmpty()){
temp.add(cur.poll());
}
}else {
cur.add(x);
while (!temp.isEmpty()){
cur.add(temp.poll());
}
}
}
public int pop() {
if(temp.isEmpty()){
return cur.poll();
}else {
return temp.poll();
}
}
public int top() {
if(temp.isEmpty()){
return cur.peek();
}else {
return temp.peek();
}
}
public boolean empty() {
return temp.isEmpty()&&cur.isEmpty();
}
}

View File

@ -0,0 +1,120 @@
package com.markilue.leecode.stackAndDeque.second;
import java.util.Deque;
import java.util.LinkedList;
import java.util.Stack;
/**
*@BelongsProject: Leecode
*@BelongsPackage: com.markilue.leecode.stackAndDeque.second
*@Author: dingjiawen
*@CreateTime: 2023-01-02 13:07
*@Description:
* TODO 二刷力扣20题 有效的括号:
* 给定一个只包括 '('')''{''}''['']'的字符串 s 判断字符串是否有效
* <p>
* 有效字符串需满足
* 左括号必须用相同类型的右括号闭合
* 左括号必须以正确的顺序闭合
* 每个右括号都有一个对应的相同类型的左括号
*@Version: 1.0
*/
public class T03_IsValid {
/**
* 思路:本质上是一个左右匹配,见词就消的思路使用栈实现
* 速度击败98.79%内存击败58.57%
* @param s
* @return
*/
public boolean isValid(String s) {
char[] chars = s.toCharArray();
Stack<Character> stack = new Stack<>();
for (char aChar : chars) {
if(aChar=='}'){
if(stack.isEmpty()||stack.pop()!='{'){
return false;
}
}else if(aChar==')'){
if(stack.isEmpty()||stack.pop()!='('){
return false;
}
}else if(aChar==']'){
if(stack.isEmpty()||stack.pop()!='['){
return false;
}
}else {
stack.push(aChar);
}
}
if(stack.isEmpty()){
return true;
}else {
return false;
}
}
/**
* 代码随想方法直接见左加右,相对较为简洁
* @param s
* @return
*/
public boolean isValid1(String s) {
Deque<Character> deque = new LinkedList<>();
char ch;
for (int i = 0; i < s.length(); i++) {
ch = s.charAt(i);
//碰到左括号就把相应的右括号入栈
if (ch == '(') {
deque.push(')');
}else if (ch == '{') {
deque.push('}');
}else if (ch == '[') {
deque.push(']');
} else if (deque.isEmpty() || deque.peek() != ch) {
return false;
}else {//如果是右括号判断是否和栈顶元素匹配
deque.pop();
}
}
//最后判断栈中元素是否匹配
return deque.isEmpty();
}
/**
* 官方最快:较为巧妙,使用数组模拟栈
* 速度击败100%内存击败45.42%
* @param s
* @return
*/
public boolean isValid2(String s){
if(s == null || s.length()==0){
return true;
}
char[] str = s.toCharArray();
int N = str.length;
int size = 0;
char[] stack=new char[N];
for (int i=0;i<N;i++){
char cha = str[i];
if(cha=='('||cha=='['|| cha=='{'){
stack[size++] = cha =='('?')':(cha=='['?']':'}');
}else {
if(size == 0){
return false;
}
char last = stack[--size];
if(cha != last ){
return false;
}
}
}
return size == 0;
}
}

View File

@ -13,7 +13,7 @@ import java.util.Arrays;
* 给定一个非空的字符串 s 检查是否可以通过由它的一个子串重复多次构成
* @Version: 1.0
*/
public class RepeatedSubstringPattern {
public class T07_RepeatedSubstringPattern {
@Test
public void test() {

View File

@ -0,0 +1,107 @@
package com.markilue.leecode.string.second;
import org.junit.Test;
/**
*@BelongsProject: Leecode
*@BelongsPackage: com.markilue.leecode.string.second
*@Author: dingjiawen
*@CreateTime: 2023-01-02 10:33
*@Description:
* TODO 二刷力扣459题 重复的子字符串:
* 给定一个非空的字符串 s 检查是否可以通过由它的一个子串重复多次构成
*@Version: 1.0
*/
public class T07_RepeatedSubstringPattern {
@Test
public void test() {
String s = "abcabcdbc";
System.out.println(repeatedSubstringPattern(s));
}
@Test
public void test1() {
String s = "aba";
System.out.println(repeatedSubstringPattern(s));
}
@Test
public void test2() {
String s = "aabaaba";
System.out.println(repeatedSubstringPattern(s));
}
/**
* 思路:重复子字符串,可以从其next数组进行判断如果可以重复其next数组最后一位长度就是最后的长度
* 如果不做前面的StrStr可能还是想不到
* 速度击败76.23%内存击败69.88% 9ms
* @param s
* @return
*/
public boolean repeatedSubstringPattern(String s) {
int[] next = getNext(s);
int len = next.length;
if (next[len - 1] != 0 && len % (len - next[len - 1]) == 0) {
//刚好能整除
return true;
} else {
return false;
}
}
public int[] getNext(String c) {
int index = 0;
int[] next = new int[c.length()];
next[0] = 0;
for (int i = 1; i < c.length(); i++) {
while (index > 0 && c.charAt(i) != c.charAt(index)) {
//如果当前两数不相等,寻找 上次的相同前缀的位置next[index-1] 的下一个字符是否相等
index = next[index - 1];
}
if (c.charAt(i) == c.charAt(index)) {
index++;
}
next[i] = index;
}
return next;
}
/**
* 官方最快:在字符串前面加一个空格
* 速度击败90.73%内存击败14.30% 5ms
* @param s
* @return
*/
public boolean repeatedSubstringPattern1(String s) {
//一个用contain, 一个用KMP
int len = s.length();
s = ' ' + s;
char[] ch = s.toCharArray();
int[] next = new int[len + 1];
for(int i = 2, j = 0; i <= len; i++){
while(j > 0 && ch[i] != ch[j + 1]){
j = next[j];
}
if(ch[i] == ch[j + 1]){
j++;
}
next[i] = j;
}
if(next[len] > 0 && len % (len - next[len]) == 0){
return true;
}
return false;
}
}