leecode更新
This commit is contained in:
parent
38fe47b849
commit
d10746b2f2
|
|
@ -1,6 +1,5 @@
|
|||
package com.markilue.leecode.backtrace;
|
||||
|
||||
import com.markilue.leecode.stackAndDeque.EvalRPN;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.*;
|
||||
|
|
|
|||
|
|
@ -1,9 +1,7 @@
|
|||
package com.markilue.leecode.backtrace;
|
||||
|
||||
import com.markilue.leecode.stackAndDeque.EvalRPN;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.print.DocFlavor;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -0,0 +1,105 @@
|
|||
package com.markilue.leecode.stackAndDeque;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Deque;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Stack;
|
||||
|
||||
/**
|
||||
*@BelongsProject: Leecode
|
||||
*@BelongsPackage: com.markilue.leecode.stackAndDeque
|
||||
*@Author: dingjiawen
|
||||
*@CreateTime: 2023-01-03 10:24
|
||||
*@Description:
|
||||
* TODO 力扣104题 删除组字符串中的所有相邻重复项
|
||||
* 给出由小写字母组成的字符串 S,重复项删除操作会选择两个相邻且相同的字母,并删除它们。
|
||||
* 在 S 上反复执行重复项删除操作,直到无法继续删除。
|
||||
* 在完成所有重复项删除操作后返回最终的字符串。答案保证唯一。
|
||||
*@Version: 1.0
|
||||
*/
|
||||
public class T04_RemoveDuplicates {
|
||||
|
||||
@Test
|
||||
public void test(){
|
||||
String s= "abbaca";
|
||||
System.out.println(removeDuplicates(s));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 思路:类似消消乐,可以使用一个栈装,如果和栈顶一样就消栈,否则入栈
|
||||
* 速度击败56.41%,内存击败62.57% 44ms
|
||||
* @param s
|
||||
* @return
|
||||
*/
|
||||
public String removeDuplicates(String s) {
|
||||
|
||||
Deque<Character> stack = new LinkedList<Character>();
|
||||
char[] chars = s.toCharArray();
|
||||
|
||||
for (char aChar : chars) {
|
||||
if (!stack.isEmpty() && stack.peek() == aChar) {
|
||||
stack.pop();
|
||||
} else {
|
||||
stack.push(aChar);
|
||||
}
|
||||
}
|
||||
StringBuilder builder =new StringBuilder();
|
||||
//stack全pop出,组成字符串
|
||||
while (!stack.isEmpty()){
|
||||
builder.insert(0,stack.pop());
|
||||
}
|
||||
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 代码随想录拿字符串做栈
|
||||
* 速度击败71.16%,内存击败96.29% 22ms
|
||||
* @param s
|
||||
* @return
|
||||
*/
|
||||
public String removeDuplicates1(String s) {
|
||||
// 将 res 当做栈
|
||||
// 也可以用 StringBuilder 来修改字符串,速度更快
|
||||
// StringBuilder res = new StringBuilder();
|
||||
StringBuffer res = new StringBuffer();
|
||||
// top为 res 的长度
|
||||
int top = -1;
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
char c = s.charAt(i);
|
||||
// 当 top > 0,即栈中有字符时,当前字符如果和栈中字符相等,弹出栈顶字符,同时 top--
|
||||
if (top >= 0 && res.charAt(top) == c) {
|
||||
res.deleteCharAt(top);
|
||||
top--;
|
||||
// 否则,将该字符 入栈,同时top++
|
||||
} else {
|
||||
res.append(c);
|
||||
top++;
|
||||
}
|
||||
}
|
||||
return res.toString();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 评论区方法:使用相同了就覆盖,类似于快慢指针
|
||||
* 速度击败96.3%,内存击败74.66% 4ms
|
||||
* @param S
|
||||
* @return
|
||||
*/
|
||||
public String removeDuplicates2(String S) {
|
||||
char[] s = S.toCharArray();
|
||||
int top = -1;
|
||||
for (int i = 0; i < S.length(); i++) {
|
||||
if (top == -1 || s[top] != s[i]) {
|
||||
s[++top] = s[i];
|
||||
} else {
|
||||
top--;
|
||||
}
|
||||
}
|
||||
return String.valueOf(s, 0, top + 1);
|
||||
}
|
||||
}
|
||||
|
|
@ -17,7 +17,7 @@ import java.util.*;
|
|||
* 可以保证给定的逆波兰表达式总是有效的。换句话说,表达式总会得出有效数值且不存在除数为 0 的情况。
|
||||
* @Version: 1.0
|
||||
*/
|
||||
public class EvalRPN {
|
||||
public class T05_EvalRPN {
|
||||
|
||||
@Test
|
||||
public void test(){
|
||||
|
|
@ -9,12 +9,14 @@ import java.util.*;
|
|||
* @BelongsPackage: com.markilue.leecode.stackAndDeque
|
||||
* @Author: dingjiawen
|
||||
* @CreateTime: 2022-09-14 09:31
|
||||
* @Description: TODO 力扣239题 滑动窗口最大值:
|
||||
* 给你一个整数数组 nums,有一个大小为k的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口内的 k个数字。滑动窗口每次只向右移动一位。
|
||||
* @Description:
|
||||
* TODO 力扣239题 滑动窗口最大值:
|
||||
* 给你一个整数数组 nums,有一个大小为k的滑动窗口从数组的最左侧移动到数组的最右侧。
|
||||
* 你只可以看到在滑动窗口内的 k个数字。滑动窗口每次只向右移动一位。
|
||||
* 返回 滑动窗口中的最大值 。
|
||||
* @Version: 1.0
|
||||
*/
|
||||
public class MaxSlidingWindow {
|
||||
public class T06_MaxSlidingWindow {
|
||||
|
||||
|
||||
@Test
|
||||
|
|
@ -0,0 +1,162 @@
|
|||
package com.markilue.leecode.stackAndDeque.second;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Stack;
|
||||
|
||||
/**
|
||||
*@BelongsProject: Leecode
|
||||
*@BelongsPackage: com.markilue.leecode.stackAndDeque.second
|
||||
*@Author: dingjiawen
|
||||
*@CreateTime: 2023-01-03 11:27
|
||||
*@Description:
|
||||
* TODO 力扣150题 逆波兰表达式求值:
|
||||
* 根据 逆波兰表示法,求表达式的值。
|
||||
* 有效的算符包括+、-、*、/。每个运算对象可以是整数,也可以是另一个逆波兰表达式。
|
||||
* 注意两个整数之间的除法只保留整数部分。
|
||||
* 可以保证给定的逆波兰表达式总是有效的。换句话说,表达式总会得出有效数值且不存在除数为 0 的情况。
|
||||
*@Version: 1.0
|
||||
*/
|
||||
public class T05_EvalRPN {
|
||||
|
||||
@Test
|
||||
public void test(){
|
||||
String[] tokens = {"2", "1", "+", "3", "*"};
|
||||
System.out.println(evalRPN(tokens));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test1(){
|
||||
String[] tokens = {"4","13","5","/","+"};
|
||||
System.out.println(evalRPN2(tokens));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 思路:用stack装
|
||||
* 速度击败54.45%,内存击败10.19% 6ms
|
||||
* @param tokens
|
||||
* @return
|
||||
*/
|
||||
public int evalRPN(String[] tokens) {
|
||||
|
||||
Stack<Integer> stack = new Stack<>();//存数
|
||||
|
||||
for (String token : tokens) {
|
||||
|
||||
if(token.equals("+")){
|
||||
Integer num2 = stack.pop();
|
||||
Integer num1 = stack.pop();
|
||||
int value=num1+num2;
|
||||
stack.push(value);
|
||||
}else if(token.equals("-")){
|
||||
Integer num2 = stack.pop();
|
||||
Integer num1 = stack.pop();
|
||||
int value=num1-num2;
|
||||
stack.push(value);
|
||||
}else if(token.equals("*")){
|
||||
Integer num2 = stack.pop();
|
||||
Integer num1 = stack.pop();
|
||||
int value=num1*num2;
|
||||
stack.push(value);
|
||||
}else if(token.equals("/")){
|
||||
Integer num2 = stack.pop();
|
||||
Integer num1 = stack.pop();
|
||||
int value=num1/num2;
|
||||
stack.push(value);
|
||||
}else {
|
||||
stack.push(Integer.valueOf(token));
|
||||
}
|
||||
}
|
||||
|
||||
return stack.peek();
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 官方的规范写法
|
||||
* @param tokens
|
||||
* @return
|
||||
*/
|
||||
public int evalRPN1(String[] tokens) {
|
||||
|
||||
Stack<Integer> stack = new Stack<>();
|
||||
|
||||
ArrayList<String> list = new ArrayList<>(Arrays.asList("+", "-", "*", "/"));
|
||||
|
||||
for (String token : tokens) {
|
||||
if(!list.contains(token)){
|
||||
stack.push(Integer.valueOf(token));
|
||||
}else {
|
||||
int num2=stack.pop();
|
||||
int num1=stack.pop();
|
||||
if(token.equals("+")){
|
||||
stack.push(num1+num2);
|
||||
}else if(token.equals("-")){
|
||||
stack.push(num1-num2);
|
||||
}else if(token.equals("*")){
|
||||
stack.push(num1*num2);
|
||||
}else if(token.equals("/")){
|
||||
stack.push(num1/num2);
|
||||
}
|
||||
}
|
||||
}
|
||||
return stack.pop();
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 官方最快:
|
||||
* 速度击败100%,内存击败31.2% 1ms
|
||||
*/
|
||||
int index;
|
||||
String[] tokens;
|
||||
|
||||
public int evalRPN2(String[] tokens) {
|
||||
this.tokens = tokens;
|
||||
index = tokens.length - 1;
|
||||
return recursion();
|
||||
}
|
||||
//递归模拟栈
|
||||
public int recursion() {
|
||||
String sub = tokens[index];
|
||||
switch (sub) {
|
||||
case "+" : {
|
||||
index--;
|
||||
int right = recursion();
|
||||
index--;
|
||||
int left = recursion();
|
||||
return left + right;
|
||||
}
|
||||
case "-" : {
|
||||
index--;
|
||||
int right = recursion();
|
||||
index--;
|
||||
int left = recursion();
|
||||
return left - right;
|
||||
}
|
||||
case "*" : {
|
||||
index--;
|
||||
int right = recursion();
|
||||
index--;
|
||||
int left = recursion();
|
||||
return left * right;
|
||||
}
|
||||
case "/" : {
|
||||
index--;
|
||||
int right = recursion();
|
||||
index--;
|
||||
int left = recursion();
|
||||
return left / right;
|
||||
}
|
||||
default : {
|
||||
return Integer.parseInt(sub);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,156 @@
|
|||
package com.markilue.leecode.stackAndDeque.second;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
*@BelongsProject: Leecode
|
||||
*@BelongsPackage: com.markilue.leecode.stackAndDeque.second
|
||||
*@Author: dingjiawen
|
||||
*@CreateTime: 2023-01-03 12:00
|
||||
*@Description:
|
||||
* TODO 力扣239题 滑动窗口最大值:
|
||||
* 给你一个整数数组 nums,有一个大小为k的滑动窗口从数组的最左侧移动到数组的最右侧。
|
||||
* 你只可以看到在滑动窗口内的 k个数字。滑动窗口每次只向右移动一位。
|
||||
* 返回 滑动窗口中的最大值 。
|
||||
*@Version: 1.0
|
||||
*/
|
||||
public class T06_MaxSlidingWindow {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
int[] nums = {1, 3, -1, -3, 5, 3, 6, 7};
|
||||
int k = 3;
|
||||
System.out.println(Arrays.toString(maxSlidingWindow(nums, k)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test1() {
|
||||
int[] nums = {1, -1};
|
||||
int k = 1;
|
||||
System.out.println(Arrays.toString(maxSlidingWindow(nums, k)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 思路:维护一个优先队列(单调的),每次淘汰过期的元素即可
|
||||
* 速度18.32%,内存击败33.44%
|
||||
* @param nums
|
||||
* @param k
|
||||
* @return
|
||||
*/
|
||||
public int[] maxSlidingWindow(int[] nums, int k) {
|
||||
|
||||
int[] result = new int[nums.length - k + 1];
|
||||
PriorityQueue<int[]> deque = new PriorityQueue<>(new Comparator<int[]>() {
|
||||
@Override
|
||||
public int compare(int[] o1, int[] o2) {
|
||||
return o1[0] != o2[0] ? o2[0] - o1[0] : o2[1] - o1[1];
|
||||
}
|
||||
});//<value,index>
|
||||
|
||||
|
||||
//构造第一个窗口
|
||||
int max = nums[0]; //维护窗口最大值
|
||||
int index = -1;
|
||||
for (int i = 0; i < k; i++) {
|
||||
deque.offer(new int[]{nums[i], i});
|
||||
if (max < nums[i]) max = nums[i];
|
||||
}
|
||||
result[++index] = max;
|
||||
|
||||
for (int i = k; i < nums.length; i++) {
|
||||
deque.offer(new int[]{nums[i], i});
|
||||
while (i - k >= deque.peek()[1]) {
|
||||
deque.poll();
|
||||
}
|
||||
result[++index] = deque.peek()[0];
|
||||
}
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 代码随想录思路:维护一个双端队列,然后使其单调,每次加入时淘汰过窗口的和非单调的
|
||||
* 速度击败83.77%,内存击败48.77%
|
||||
* @param nums
|
||||
* @param k
|
||||
* @return
|
||||
*/
|
||||
public int[] maxSlidingWindow1(int[] nums, int k) {
|
||||
ArrayDeque<Integer> deque = new ArrayDeque<>();
|
||||
int n = nums.length;
|
||||
int[] res = new int[n - k + 1];
|
||||
int idx = 0;
|
||||
for(int i = 0; i < n; i++) {
|
||||
// 根据题意,i为nums下标,是要在[i - k + 1, i] 中选到最大值,只需要保证两点
|
||||
// 1.队列头结点需要在[i - k + 1, i]范围内,不符合则要弹出
|
||||
while(!deque.isEmpty() && deque.peek() < i - k + 1){
|
||||
deque.poll();
|
||||
}
|
||||
// 2.既然是单调,就要保证每次放进去的数字要比末尾的都大,否则也弹出
|
||||
while(!deque.isEmpty() && nums[deque.peekLast()] < nums[i]) {
|
||||
deque.pollLast();
|
||||
}
|
||||
|
||||
deque.offer(i);
|
||||
|
||||
// 因为单调,当i增长到符合第一个k范围的时候,每滑动一步都将队列头节点放入结果就行了
|
||||
if(i >= k - 1){
|
||||
res[idx++] = nums[deque.peek()];
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 官方最快:理论上来说是最基础的方法,只不过可能剪枝效果太好,看起来速度很多
|
||||
* 极端情况下需要一直在窗口中for来寻找最大值,时间复杂度直接O(k*n)
|
||||
* 速度击败100%,内存击败99.59%
|
||||
* @param nums
|
||||
* @param k
|
||||
* @return
|
||||
*/
|
||||
public int[] maxSlidingWindow2(int[] nums, int k) {
|
||||
int n = nums.length;
|
||||
int left = 0, right = k - 1;
|
||||
// 当前的最大值的下标
|
||||
int pre = -1;
|
||||
int max = Integer.MIN_VALUE; // min - 1 = max
|
||||
int[] ans = new int[n - k + 1];
|
||||
|
||||
while (right < n) {
|
||||
// 滑动窗口left-right
|
||||
if (left <= pre) {
|
||||
// 窗口滑动前的最大值,还在窗口里面
|
||||
// 比较上一轮最大值和窗口新增的值
|
||||
if (nums[right] >= nums[pre]) {
|
||||
pre = right;
|
||||
max = nums[right];
|
||||
}
|
||||
} else if (nums[right] >= max - 1 ) {
|
||||
//max-1是因为max是right-left中最大的,如果有相同的他肯定是最后一个
|
||||
//所以这个数后面的数如果不是最大值,那么肯定<=max-1
|
||||
pre = right;
|
||||
max = nums[right];
|
||||
} else if (nums[left] >= max - 1) {
|
||||
pre = left;
|
||||
max = nums[left];
|
||||
} else {
|
||||
//既不是最左边又不是最右边,就需要手动求最大值
|
||||
max = Integer.MIN_VALUE;
|
||||
// 求当前窗口的最大值
|
||||
for (int i = left; i <= right; i++) {
|
||||
if (nums[i] >= max) {
|
||||
max = nums[i];
|
||||
pre = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
ans[left] = max;
|
||||
left++;
|
||||
right++;
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
}
|
||||
|
|
@ -6,7 +6,8 @@ import numpy as np
|
|||
import random
|
||||
import pandas as pd
|
||||
import seaborn as sns
|
||||
from condition_monitoring.data_deal.loadData import read_data
|
||||
import scipy.signal
|
||||
|
||||
|
||||
'''
|
||||
@Author : dingjiawen
|
||||
|
|
@ -193,17 +194,6 @@ def test_mse(mse_file_name:str=mse_file_name,max_file_name:str=max_file_name):
|
|||
|
||||
|
||||
|
||||
|
||||
def test_corr(file_name=source_path,N=10):
|
||||
needed_data, label = read_data(file_name=file_name, isNew=False)
|
||||
print(needed_data)
|
||||
print(needed_data.shape)
|
||||
# plot_original_data(needed_data)
|
||||
person = plot_Corr(needed_data, label)
|
||||
person = np.array(person)
|
||||
pass
|
||||
|
||||
|
||||
def plot_raw_data():
|
||||
# data, label = read_data(file_name='G:\data\SCADA数据\jb4q_8_delete_total_zero.csv', isNew=False)
|
||||
# data = np.loadtxt('G:\data\SCADA数据/normalization.csv',delimiter=',')
|
||||
|
|
@ -218,7 +208,14 @@ if __name__ == '__main__':
|
|||
# test_mse()
|
||||
# test_result()
|
||||
# test_corr()
|
||||
plot_raw_data()
|
||||
# plot_raw_data()
|
||||
data=np.load("H:\data\predict_data\\HI_merge_data.npy")
|
||||
print(data)
|
||||
data=data[:1250,1]
|
||||
plt.plot(data)
|
||||
data_smooth=scipy.signal.savgol_filter(data,53,3)
|
||||
plt.plot(data_smooth)
|
||||
plt.show()
|
||||
pass
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue